April 7, 2011

Code Example Creating a Custom Validation Attribute in ASP.NET MVC 2.0


The System.ComponentModel.DataAnnotations namespace includes a number of built-in validation attributes .
So far(In Previous Example) We have used  [Required], [StringLength], and [RegularExpression].
We can also create  own custom validation attributes by deriving from the ValidationAttribute base class within the System.ComponentModel.DataAnnotations namespace.  
Or We can  derive from any of the existing validation attributes just to modify the  functionality. 
So We Will Include One  more class in Previous StudentModel.cs


Now this Custom Validator can be used Like this


Before I was using Regular Expression Validation Attribute to validate the email.
Now I am using Email Attribute which is Custom Validation Attribute
Now To test it enter Wrong Email and see the Custom Validation Attribute in action.

No comments:

Post a Comment