Thursday, March 1, 2012

Extend the built-in ASP.NET DataAnnotations using DataAnnotations Extensions

If you have worked with development of web service, silverlight application or mvc applicaiton then you know about DataAnnotations class to validate DTO client side as well as server side.

DataAnnotations contains mostly Required, Range, RegularExpression and StringLength and to see full list of attributes it contains visit this msdn link "Using Data Annotations to Customize Data Classes" .

In my mvc application there is person class (Model) and validation for email as per below (by regular expression)



Scott Kirkland has extended this and created excellent DataAnnotations Extensions pacakge for .net to allow more validation attributes, also he has explained about this in his blog post with examples.

so after installing DataAnnotations Extensions Nuget package i can write email validation as per below





Following are list of more validation attributes available in  DataAnnotations Extensions
  •     CreditCardAttribute
  •     CuitAttribute
  •     DateAttribute
  •     DigitsAttribute
  •     EmailAttribute
  •     EqualToAttribute
  •     FileExtensionsAttribute
  •     IntegerAttribute
  •     MaxAttribute
  •     MinAttribute
  •     NumericAttribute
  •     UrlAttribute
  •     YearAttribute
You can see demo of each & every attributes here.

No comments: