25 November 2008

C# Regex Number Validation With Regular Expression

In order to check whether an input is a valid number or not,
regular expressions
are very easy to use to validate the input value.

The sample function below show how to check if the input is number or not, code is in C#.

using System.Text.RegularExpressions;

public static bool IsItNumber(string inputvalue)
{
Regex isnumber = new Regex("[^0-9]");
return !isnumber.IsMatch(inputvalue);
}
IsItNumber("2"); will return true;
IsItNumber("A"); will return false;

Is number validation with regular expression in C#.

7 comments:

Unknown said...

THANKS

HermanW said...

Tanks you,
your code can help me .....

ajay dhole said...

thanks alot...!!!

Bharat said...

Gracias .....

Pravin said...

Thanks....
Very Nice code

Matthew Fournier(matty0913/n3wm477y) said...

This will do the same thing and requires less logic:

^[0-9]+$

Anonymous said...

Thanks, it helped me a lot.