How to validate for numbers without using keypress option
why isnt Char.IsNumber
or .IsDigit
working
or should I use regex expression for validation
private bool ValidateContact()
{
if (Char.IsNumber(textBox4.Text)){
return true;
}
You could simply parse the number:
private bool ValidateContact()
{
int val;
if (int.TryParse(textBox4.Text, out val))
{
return true;
}
else
{
return false;
}
}
You are trying to call a method that is written for char
for string
. You have to do them all separately, or use a method that is much easier to use, like the above code.
why isnt Char.IsNumber or .IsDigit working
because Char.IsDigit
wants a char
not a string
. So you could check all characters:
private bool ValidateContact()
{
return textBox4.Text.All(Char.IsDigit);
}
or - better because IsDigit
includes unicode characters - use int.TryParse
:
private bool ValidateContact()
{
int i;
return int.TryParse(textBox4.Text, out i);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With