Introduction
In .Net 4.0, namespace System.ComponentModel.DataAnnotations allows you to create new attribute and with the help of this you can validate the data as per your requirement.
How to Do
ValidationAttribute is abstract class and it is use as base class of various inbuilt validation attribute like required, stringlength, etc. With help of ValidationAttribute class we can create our own new validation attribute.
The following are the steps to create a new Validation.
Step 1: Create a class which inherits from ValidationAttribute:
public sealed class MyRequiredAttribute : ValidationAttribute
{
....
....
}
Step 2: The validation attribute class has a method called "IsValid" to validate data. If you want to do custom validation then you must override this method:
public sealed class MyRequiredAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
var required = new RequiredAttribute();
return required.IsValid(Convert.ToString(value).Trim());
}
}
Example
In this article, I have created a new attribute, named "MyRequiredAttribute". Internally I have called the builtin attribute "RequiredAttribute", but instead of this you can write your custom code or logic to create a validation attribute.
I have also created another example to create a new attribute for Email. In the bottom of this attribute, I used "RegularExpressionAttribute" to validate the email address.
public sealed class EmailAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
return new RegularExpressionAttribute(@"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$").IsValid(Convert.ToString(value).Trim());
}
}
public class DataClass
{
[Email]
public string EmailAddress { get; set; }
[MyRequiredAttribute(ErrorMessage = "Jignesh Test")]
public string Password { get; set; }
}
Conclusion
System.ComponentModel.DataAnnotations allow us to create new validation attributes to write custom logic or code for validation. In MVC, creation of new attributes that are automatically picked up by the validation process of the DefaultModelBinder is easy.

spy spiderPosted Oct 4, 2018, 7:46 AM
You source code is corrupted though
Nilesh MakavanaPosted Aug 4, 2018, 3:16 PM
How to perform data annotation for multiple company, let say I have two company where i have one filed for company A is mandatory, and same filed is non-mandatory for company B. I tried all but the issue is the first loaded validation only works, for another company the model validation only not goes in debug. please suggest what to do.
Jignesh TrivediPosted Jun 28, 2012, 12:56 AM
Yes Gaurav. Its really good idea beacuse the Data Annotation validators is that they enable us to perform validation simply by adding one or more attributes like the Required or String Length attribute – on the class property. this will help to reduce code size as well as code reusability. Also MVC, EF and WCF inbuilt support for this.
Gaurav GuptaPosted Jun 28, 2012, 12:43 AM
Is Data Annotations really a good idea for validation?