validate Integers.....

Jun 20 2007 11:31 PM
Hi all c# lovers,

I want to check the input string ( to a textbox) is a numeric value or not. what is the easiest method to do this?  can someone give me a sample code for that... I hope .net has some methods to validate these formats.


thank you....

Answers (2)

0
Alan

Alan

  • 0
  • 5.8k
  • 0
Jun 21 2007 4:52 AM

Thought I'd just add that the TryParse() methods are only available in .NET 2.0 or later.

If you're using .NET 1.0 or 1.1, you can replicate it with:

int i;

bool isValid = true;

try
{
     i = Int32.Parse(textBox1.Text);
}
catch
{
    isValid = false;
    i = 0;
}

0
Jan Montano

Jan Montano

  • 0
  • 2k
  • 0
Jun 20 2007 11:34 PM
use Int32.TryParse.

int i;
if (Int32.TryParse("123", out i))
{
// numeric
}
else
{
// not numeric
}