Loading ...

Validation with Regular Expressions Made Simple | CodeAsp.Net

Validation with Regular Expressions Made Simple

0 (0votes)


    Here i am wirtting this bolg because, .net has great support for regular expressions, and it's hard to do input validation without them.My breaking point came this past 15days, as i wrote yet another coplicated method to comma delimited lists of integers.

    Well, for one thing, just about every article and book out there is geared towords using regular expression in text searches, rather than input validation. Here we can see following resular exression, using this regular exporession you can validate any input data:

Matches any signed integer


INTEGER_PATTERN = @"^(\+|-)?\d+$"   

Description This matches any real number, with optional decimal point and numbers after the decimal, and optional positive (+) or negative (-) designation.

 Matches 123 | -123.45 | +123.56

 Non-Matches 123x | .123 | -123.

 Matches any signed integer 

DECIMAL_PATTERN = @"^[-+]?\d+(\.\d+)?$" 

A regular expression that matches numbers. Integers or decimal

numbers with or without the exponential form.

 Matches 23 | -17.e23 | +.23e+2  

Non-Matches +.e2 | 23.17.5 | 10e2.0  

NUMBER_PATTERN = @"^[+-]?([0-9]*\.?[0-9]+|[0-9]+\.?[0-9]*)([eE][+-]?[0-9]+)?$"


 Regular expression for  Natural Nyumber

NATURAL_NUMBER_PATTERN = @"^[0-9]*[1-9]+$|^[1-9]+[0-9]*$"

 Regular expression for whole Natural Nyumber


 all positive real numbers (no exponential numbers, no "+" sign)


Description: Accept only (0-9) integer and one decimal point(decimal point is also optional).After decimal point it accepts at least one numeric .This will be usefull in money related fields or decimal fields.


Matches .568 | 8578 | 1234567.1234567 


Non-Matches 568. | 56.89.36 |

POSITIVE_NUMBER_PATTERN = @"^\d+(\.\d+)?$"

Description: This expression validates a alpha bates only

from a to z and A to Z


Matches Asp | TEST


Non-Matches as123 | 234Test




Description: This expression validates a alpha bates and Number

from a to z and A to Z and 0 to 9


Matches Asp | TEST | as123


Non-Matches 1234@#$ | ASP@#$^$




Define Email Constant

VALID_EMAIL_PATTERN = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"


Description: This expression validates a date field in dd/mm/yyyy

and mm/dd/yyyy format


Matches 08/24/2010 | 24/08/2010


Non-Matches 08/24/2010 11:24:34 | 14/24/2010


  VALID_DATE_PATTERN = @"^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((19|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))|^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d+$" 


Description: This expression validates a Temperature in Celsius,

Fahrenheit and KELVIN format


Matches 34F | 56 Celsius | 22C


Non-Matches 26Fer | 47 DegC 

VALID_TEMPERATURE_PATTERN = @"^[-+]?\d*(\.\d+)?( )?(Celsius|C|c|CELSIUS|Fahrenheit|F|f|FAHRENHEIT|Kelvin|K|k|KELVIN)?$"


Description: This expression validates a any key with special character also

Matches 34F | !@#$ | %^#$


 VALID_CHECK_ANY = @"[ 0-9A-Za-z\:\/\\\&\%\*\{\}\(\),.\-@+#_<>?~`!|=]$"




Comments (2)

Nice post. Just one thing.. It would be nice to read if you will format the codes.
gr8 ...very helpful one...i was searching for the same helpthanks for the sharing

Top Posts