Validation In ASP.NET MVC Using DataAnnotations

Validation

To implement validation in ASP.NET MVC, you should use DataAnnotation attributes. These attributes are present in System.ComponentModel.DataAnnotations namespace. Validation attributes specifies the behaviour, which you want apply to the properties of models. ASP.NET MVC framework automatically enforces validation rules and display validation messages in View.

The list of Datannotations attibutes is given below.
  • Required- This indicates that the property is a required field.
  • StringLength- This defines a maximum length for the string field.
  • MaxLength- This specifies maximum length for the string field.
  • MinLength- This specifies minimum length for the string field.
  • Range- This defines a maximum and minimum value for the numeric field.
  • RegularExpression- This specifies that the field value must match with the specified Regular Expression.
  • CreditCard- This specifies that the specified field is a credit card number.
  • EmailAddress- This validates with an Email address format.
  • FileExtension- This validates with the file extension.
  • Phone- This specifies that the field is a phone number, using regular expression for the phone numbers.
  • CustomValidation- This specifies custom validation method to validate the field.

Now, the sample is given below. 

  1. using System.ComponentModel;  
  2. using System.ComponentModel.DataAnnotations;  
  3. using System.Web.Mvc;  
  4. public class EmployeeModel {  
  5.     public int EmpId {  
  6.         get;  
  7.         set;  
  8.     }  
  9.     [DisplayName("Employee Name")]  
  10.     [Required(ErrorMessage = "Employee Name is required")]  
  11.     [StringLength(100, MinimumLength = 3)]  
  12.     public String EmpName {  
  13.         get;  
  14.         set;  
  15.     }  
  16.     [Required(ErrorMessage = "Employee Address is required")]  
  17.     [StringLength(300)]  
  18.     public string Address {  
  19.         get;  
  20.         set;  
  21.     }  
  22.     [Required(ErrorMessage = "Salary is required")]  
  23.     [Range(3000, 10000000, ErrorMessage = "Salary must be between 3000 and 10000000")]  
  24.     public int Salary {  
  25.         get;  
  26.         set;  
  27.     }  
  28.     [Required(ErrorMessage = "Please enter your email address")]  
  29.     [DataType(DataType.EmailAddress)]  
  30.     [Display(Name = "Email address")]  
  31.     [MaxLength(50)]  
  32.     [RegularExpression(@ "[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Please enter correct email")]  
  33.     public string Email {  
  34.         get;  
  35.         set;  
  36.     }  
  37. }  
  38. }   

Now, these will be automatically used in view by HTML helpers.

  1. @model EmployeeModel  
  2. @ {  
  3.     ViewBag.Title = "Employee Details";  
  4.     Layout = "~/Views/Shared/_Layout.cshtml";  
  5. }  
  6. @using(Html.BeginForm()) { < div class = "editor-label" > @Html.LabelFor(m => m.EmpName) < /div> < div class = "editor-field" > @Html.TextBoxFor(m => m.EmpName)  
  7.     @Html.ValidationMessageFor(m => m.EmpName) < /div> < div class = "editor-label" > @Html.LabelFor(m => m.Address) < /div> < div class = "editor-field" > @Html.TextBoxFor(m => m.Address)  
  8.     @Html.ValidationMessageFor(m => m.Address) < /div> < div class = "editor-label" > @Html.LabelFor(m => m.Salary) < /div> < div class = "editor-field" > @Html.TextBoxFor(m => m.Salary)  
  9.     @Html.ValidationMessageFor(m => m.Salary) < /div> < div class = "editor-label" > @Html.LabelFor(m => m.Email) < /div> < div class = "editor-field" > @Html.TextBoxFor(m => m.Email)  
  10.     @Html.ValidationMessageFor(m => m.Email) < /div> < p > < input type = "submit"  
  11.     value = "Save" / > < /p>  
  12. }  

Note

You can enable client side validation also. To enable client side validation, you need to add the script files given below.

  1. <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>  
  2. <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>  
Ebook Download
View all
Learn
View all