Introduction


Data Annotations is a library in the .NET Framework that helps in validation. The developer doesn't need to create the logic for the validation since the validation can be done by specifying the data annotation attributes that forces the data validation rules.

Namespace


The namespace System.ComponentModel.DataAnnotations provides the attribute classes for validations.

Attribute Classes

There are more than 30 attribute classes in the System.ComponentModel.DataAnnotations namespace. We will discuss some of the most commonly used attributes.

  • RequiredAttribute
  • Range Attribute
  • EmailAddressAttribute
  • MinLengthAttribute
  • MaxLengthAttribute
Required Attribute

The Required Attribute forces the required field validation for the specified data field.

Example
  1. [Required(ErrorMessage = "Please enter Name)]
  2.  public string Name{ getset; } 
The Error message property is used to get/set the error message.

Range Attribute

The Range attribute forces the range validation for the data field.

Example
  1. [Range(1.00, 100.00,ErrorMessage="Please enter a Amount between 1.00 and 100.00" )]  
  2. public decimal? Amount { getset; } 
EmailAddressAttribute

The Email address attribute forces the validation and checks to ensure that the field has a valid email ID.

Example
  1. [EmailAddress(ErrorMessage = "Please enter a valid Email address")]  
  2. public string email { getset; } 
Regular Expression Attribute

Regular expressions can be added to a data field. The added regular expression will be forced for validation.

Some of most commonly used snippets for validation
  1. [RegularExpression(@"^[0-9]{8,11}$", ErrorMessage = "error Message ")] 
Used for checking the field to have only numbers and min of 8 digits and max of 11 digits.
  1. [RegularExpression(@"[0-9]{0,}\.[0-9]{2}", ErrorMessage = "error Message")] 
Used for checking the field to have only numbers with 2 decimal places.
  1. [RegularExpression(@"^(?!00000)[0-9]{5,5}$", ErrorMessage = "error Message")] 
Used for validation Zip code. The Zip code should contain 5 digits and should not be 00000.
  1. [RegularExpression(@"^(\d{4})$", ErrorMessage = "error Message")] 
The field should contain 4 digit numbers. Used for limiting the characters and allowing only numbers.
  1. [RegularExpression(@"^(\d{5,9})$", ErrorMessage = "error Message")] 
The field should contain a minimum of 5 digit numbers and a maximum of 9 digit numbers.

Regex for validating Email.
  1. @"^(([^<>()[\]\\.,;:\s@""]+(\.[^<>()[\]\\.,;:\s@""]+)*)|("".+""))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$" 
  1. @"^(?!0+$)[0-9]{5,5}$" 
Used while validating the Zip codes. Also the number should not start with “0”.

MinLengthAttribute

Validates the minimum length of the data field (String/Array). It will not work for int types.

MaxLengthAttribute

Validates the maximum length of the data field (String/Array). It will not work for int types.

Enabling Unobtrusive Client-side Validation via data Annotaions

When we include the following changes in the config file, the client-side validation will be done based on the data annotation rules that we have defined in the model.
  1. <add key="ClientValidationEnabled" value="true" />   
  2. lt;add key="UnobtrusiveJavaScriptEnabled" value="true" /> 
The following scripts should be added to the view page to enable Unobtrusive Client-side Validation via data Annotations.
  1. <script src="~/Scripts/jquery-2.1.1.js"></script>  
  2. <script src="~/Scripts/jquery.validate.min.js"></script>  
  3. <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script