James Ray Anderson

James Ray Anderson
James Ray Anderson
0 comments

MVC - Handy Data Annotations with RegEx

10:00 PM
You will need System.ComponentModel.DataAnnotations for these:

Email Address
[RegularExpression("^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$", ErrorMessage = "Improperly formatted email.  Please enter a valid email.")]

Zip Code
[RegularExpression(@"(^(?!0{5})(\d{5})(?!-?0{4})(-?\d{4})?$)", ErrorMessage = "Improperly formatted zip code.  It must be entered as nnnnn or nnnnn-nnnnn.")]

Other Regular Expressions
For more great RegEx Expressions, check out the RegEx Lib


You can also check out the blog post MVC - Data Annotations Extensions

0 comments:

 
Toggle Footer
Top