ASP.Net C# isNumeric Equivalent

May 20, 2011

In C# there is no isNumeric Equivalent. And doing some testing with int.Parse left me with a bad taste in my mouth after doing some testing. I will go over testing results and let you know what I finally went with.

 

Int.Parse IMO is not a good way for checking for valid numbers. Reason being that in my testing I found that the # symbol validates for whatever reason. Everything else threw an exception.
In the end I used Regex to check for invalid numbers.

 

Regex reg = new Regex(@”^[0-9]$”);
string propertyid = int.Parse(Request.QueryString[“id”]).ToString();
if (reg.IsMatch(propertyid) == true)
{
// Do stuff here
}

 

This will definatively not allow anything other than a number.

Got something to say?

You must be logged in to post a comment.