Yogeshlcse's Blog

If You Commit to Nothing, You’ll Be Distracted By Everything

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

on July 19, 2013

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 .

[Required]

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; }

 

OR

 

[Required]

[ 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.

  [EnumDataType(typeof(Color))]

  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.

 [DataType(DataType.Password)]

public String NewPassword {get; set;} 

 

 [DataType(DataType.Password)]

[Compare(“NewPassword”])

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.

   [RegularExpression(@”\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b”)]

    public String Email { get; set; }

About these ads

One response to “Validating model Data with Data Annotations API, ASP.NET MVC4

  1. Arun says:

    Yogi can we use URL Attribute for validating URL

    Example:
    [Url(ErrorMessage = “The CourseAgendaURL is not a valid fully-qualified http or https URL.”)]
    public string CourseAgendaURL
    {
    get;
    set;
    }

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: