Validation Using Data Annotations In ASP.NET MVC 5

Introduction:

In this article we will help fresher candidates to understand how to do perform validation using Data Annotation in ASP.NET MVC 5.

Validation

Objective of this article

  1. Reduce the use of regular expression in ASP.NET MVC project.
  2. Build application with proper error messages in minimum code.

Background

In old web form to do validations, developerS use regular expression, to display error message they use different types of strings & on button click event they write their login for validation on server side so it takes a lot of time to check validation.

In MVC architecture by using namespace System.ComponentModel.DataAnnotations; we can achieve validation in minimum code. To do validation using DataAnnotations techniques here are the steps,

Step 1:

Create a simple MVC application using VS 2013. Create one controller & give name to that controller. Write two action methods in above controller as one to take inputs from user & another to check validation on post method.

  1. public ActionResult Index()   
  2. {  
  3.     return View();  
  4. }  
  5. public ActionResult GetData()  
  6. {  
  7.     return View("Index");  
  8. }  
Step 2:

Create a model as follows. To achieve validation add namespace System.ComponentModel.DataAnnotations; in model. Now as per your project requirement you can achieve validation or you can also give mandatory fields with proper data type.
  1. public class Employee   
  2. {  
  3.     [Required]  
  4.     [Display(Name = "Employee Id")]  
  5.     public int Id   
  6.     {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     [Required]  
  11.     [Display(Name = "Employee Name")]  
  12.     public string Name  
  13.     {  
  14.         get;  
  15.         set;  
  16.     }  
  17.     [Required]  
  18.     [Display(Name = "Email Id")]  
  19.     [DataType(DataType.EmailAddress)]  
  20.     public string Email   
  21.     {  
  22.         get;  
  23.         set;  
  24.     }  
  25.     [Required]  
  26.     [Display(Name = "Phone Number")]  
  27.     [DataType(DataType.PhoneNumber)]  
  28.     public string Mobile   
  29.     {  
  30.         get;  
  31.         set;  
  32.     }  
  33.     [Required]  
  34.     [Display(Name = "Postal Code")]  
  35.     [DataType(DataType.PostalCode)]  
  36.     public int PostalCode {  
  37.         get;  
  38.         set;  
  39.     }  
  40.     [Required]  
  41.     [Display(Name = "Creation Date")]  
  42.     [DataType(DataType.Date)]  
  43.     public DateTime CreateDate   
  44.     {  
  45.         get;  
  46.         set;  
  47.     }  
  48.     [Required]  
  49.     [Display(Name = "Credit Card Details")]  
  50.     [DataType(DataType.CreditCard)]  
  51.     public string Address {  
  52.         get;  
  53.         set;  
  54.     }  
  55.     [Required]  
  56.     [Display(Name = "Password")]  
  57.     [DataType(DataType.Password)]  
  58.     public string Password {  
  59.         get;  
  60.         set;  
  61.     }  
  62.     [Required]  
  63.     [Display(Name = "URL")]  
  64.     [DataType(DataType.Url)]  
  65.     public string URL  
  66.     {  
  67.         get;  
  68.         set;  
  69.     }  
  70. }  
Step 3:

Create view for above methods by simply right click on each action method.

For first view add model reference on top, some javascript files in Script folder & drag three files in the following sequence in first view after title tag as in the following,
  1. <script src="~/Scripts/jquery-1.7.1.js"></script>  
  2. <script src="~/Scripts/jquery.validate.js"></script>  
  3. <script src="~/Scripts/jquery.validate.unobtrusive.js"></script>  
Write logic to take input from user & validation summary message to show in view in body tag as follows,
  1. <div>  
  2.     @Html.ValidationSummary() @using (Html.BeginForm("GetData","Home", FormMethod.Post)) { @Html.EditorForModel()  
  3.     <br />  
  4.     <input id="submit" type="submit" /> }  
  5. </div>  
Step 4:

Now run our application & without entering any value click on submit button and you will see the following error messages.

error

Now enter valid values and then click submit.

error

Summary

This article will help fresher candidates to understand how to do validation using Data Annotation in ASP.NET MVC 5.

Hope you enjoyed this one.

 


Similar Articles