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:

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

    ReplyDelete
  2. thanks alot...!!!

    ReplyDelete
  3. Thanks....
    Very Nice code

    ReplyDelete
  4. Matthew Fournier(matty0913/n3wm477y)7/27/2012 10:18 AM

    This will do the same thing and requires less logic:

    ^[0-9]+$

    ReplyDelete
  5. Thanks, it helped me a lot.

    ReplyDelete

Note: Only a member of this blog may post a comment.