Thursday, August 14, 2008

How to check entered value in textbox is numeric or not

There are number of ways to check whether the string that has entered in the textbox is numeric or not

1. using int.Parse
Ex:
function bool stringIsNumericOrNot(string txtBoxValue)
{
bool isNumericOrNot = true;
try
{
int.Parse(TextBox1.Text);
}
catch
{
isNumericOrNot = false;
}
return isNumericOrNot;
}

2. Using Regular Expression
public bool IsNumberOrNot(String strNumber)
{
Regex regNumericPattern= new
Regex("[^0-9]");
return !regNumericPattern.IsMatch(strNumber);
}


Hope this may help you

Kumar