Validating model Data with Data Annotations API, ASP.NET MVC4

Microsoft ships a very effective and easy-to-use data validation API called Data Annotations in the .NET Framework.

 Namespace:  System.ComponentModel.DataAnnotations

Following are the inbuilt Data annotation attributes for validation provided by ASP.NET MVC4.

1. Required: Checks whether a non-null value is assigned to the property. It can be configured to fail if an empty string is assigned .


public string Title { get; set; }


[Required(ErrorMessage=”Custom Error Message”)]

public string Title { get; set; }


2. StringLength: Checks whether the string is longer than the specified value.

[Required, StringLength(50)]

public string Title { get; set; }





[ StringLength(50, MinimumLength=4)]

public string Title { get; set; }


3. Range Validation: Checks whether the value falls in the specified range. It defaults to numbers, but it can be configured to consider a range of dates, too.

[Range(1, 10000)]

public decimal StartPrice { get; set; }


[Range(typeof(DateTime), “1/1/2012”, “12/31/9999”)]

public DateTime  EndTime { get; set; }


4. EnumDataType: Checks whether the value can be matched to any of the values in the specified enumerated type.


  public Categories Category { get; set; }

public enum Color {

   Red,  Blue    



5. Custom Validation: Checks the value against the specified custom function. The CustomValidation attribute takes two parameters: a type and a method name.

[CustomValidation(typeof(Temp), “ValidateColor”)]

public Colors Color { get; set; }


public static ValidationResult ValidateColor (Colors color)

public static ValidationResult ValidateColor (Colors color, ValidationContext context)


6.Compare: Compare Checks whether two specified properties in the model have the same value. The comparison is made using the Equals method as implemented on the specific type.


public String NewPassword {get; set;} 




public String RetypePassword {get; set;}


7. Remote validation: Remote Makes an Ajax call to the server, and checks whether the value is acceptable. The Ajax call is placed every time the input field loses the focus after having been modified. The property decorated with the Remote attribute honors the client-side validation contract and doesn’t allow the form to post back until a valid value is entered .

[Remote(“CheckUserName”, “User”,  ErrorMessage=”Specifid username is already exists. Please choose other name”)]

public String UserName { get; set; }


8. RegularExpression: Checks whether the value matches the specified expression.


    public String Email { get; set; }


One thought on “Validating model Data with Data Annotations API, ASP.NET MVC4

  1. Yogi can we use URL Attribute for validating URL

    [Url(ErrorMessage = “The CourseAgendaURL is not a valid fully-qualified http or https URL.”)]
    public string CourseAgendaURL

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s