Validate Textbox only to accept numeric
-
Hi friends, how to validate textbox should accept only numeric in c#.Net. Please Help. Thanks in Advance, Regards,
Prya
-
Hi friends, how to validate textbox should accept only numeric in c#.Net. Please Help. Thanks in Advance, Regards,
Prya
You can override the
OnKeyPress
orOnValidating
methods of theTextBox
, if overriding theKeyPress
then simply filter out the keystrokes which are not numeric. If overriding theOnValidating
method then you can useint.TryParse
to check to see if it's a valid integer. There are numerous articles on the web about how to do this, the best solution in my opinion would be to override theOnKeyPress
event.
I have no idea what I just said but my intentions were sincere. Poore Design
-
You can override the
OnKeyPress
orOnValidating
methods of theTextBox
, if overriding theKeyPress
then simply filter out the keystrokes which are not numeric. If overriding theOnValidating
method then you can useint.TryParse
to check to see if it's a valid integer. There are numerous articles on the web about how to do this, the best solution in my opinion would be to override theOnKeyPress
event.
I have no idea what I just said but my intentions were sincere. Poore Design
That will work if the user didn't use the paste feature
-
Hi friends, how to validate textbox should accept only numeric in c#.Net. Please Help. Thanks in Advance, Regards,
Prya
-
Hi friends, how to validate textbox should accept only numeric in c#.Net. Please Help. Thanks in Advance, Regards,
Prya
to complete with a bit of code, what has been already so nicely said:
void TxtRegisteringKeyKeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
char[] myNum = new char[11] { '0' , '1', '2', '3', '4', '5', '6', '7', '8', '9', '\b' };
bool blnNumFnd = false;
foreach (char c in myNum)
{
if (e.KeyChar == c)
blnNumFnd = true;
}
if (!blnNumFnd)
e.KeyChar = '\0';
}Contact me! Please feel free to visit my site
-
That will work if the user didn't use the paste feature
-
Hi friends, how to validate textbox should accept only numeric in c#.Net. Please Help. Thanks in Advance, Regards,
Prya