How to validate numbers
i have mobile number field in that i type only numbers how to validate using c sharp code
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sam HobbsPosted Jan 11, 2012, 6:17 AM
I think the question whould be clarified. Do you want to validate the data as it is typed and not allow any invalid characters to be entered or do you want to validate the data after all the data is entered? Satyapriya' reply is for checking the data as it is entered and I think that Jignesh's and Ankit's reply is for after the data is entered.
I think there is also a templated textbox but as I say I do not know if the application is ASP or not or whatever.
Ankit ShivnakrPosted Jan 11, 2012, 2:53 AM
Jignesh TrivediPosted Jan 11, 2012, 2:33 AM
try this,
bool IsNumber(string text)
{
Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$");
return regex.IsMatch(text);
}
var isMatch = IsNumber(moblieno);
hope this help.
Satyapriya NayakPosted Jan 10, 2012, 11:57 AM
Try this...
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
e.Handled = true;
}
Thanks