Validation Using Data Annotation In ASP.NET MVC

In my previous article we learned how to do validation in ASP.NET Web from using Validation Control as well as DataAnnotations attributes.

You can read that article here: Validation In ASP.NET Web Form.
 
Now we will do the same thing here in ASP.NET MVC Application.
 
Step 1: Create new ASP.NET Web Application, select MVC template and click 'Ok'.
 
 
 
 
It will create Model, View and Controller folders in project.
 
 
 
Step 2: Now let's create a Model class first, for that right click on Models folder, click Add, then Class and give a name 'UserRegistration.cs'.
  
 
Step 3: Now let us create property for the following fields and implement validation for the field.
 
 
 
For that add the following code in your model class file. Remember these are the same things we have done in 'Class Library' in my previous article.
  1. public class UserRegistration    
  2. {    
  3.     [Required(ErrorMessage = "Name is required")]    
  4.     public string name    
  5.     {    
  6.         get;    
  7.         set;    
  8.     }    
  9.   
  10.     [Required(ErrorMessage = "email is required")]    
  11.     [RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9]+)*\\.([a-z]{2,4})$", ErrorMessage = "Invalid email format.")]    
  12.     public string email    
  13.     {    
  14.         get;    
  15.         set;    
  16.     } = "";    
  17.   
  18.     [Required(ErrorMessage = "Mobile is required")]    
  19.     [RegularExpression(@"\d{10}", ErrorMessage = "Please enter 10 digit Mobile No.")]    
  20.     public string mobile    
  21.     {    
  22.         get;    
  23.         set;    
  24.     }    
  25.   
  26.     [Required(ErrorMessage = "Password is required")]    
  27.     [DataType(DataType.Password)]    
  28.     public string Password    
  29.     {    
  30.         get;    
  31.         set;    
  32.     }    
  33.   
  34.     [Required(ErrorMessage = "Confirm Password is required")]    
  35.     [DataType(DataType.Password)]    
  36.     [Compare("Password")]    
  37.     public string ConfirmPassword    
  38.     {    
  39.         get;    
  40.         set;    
  41.     }    
  42.   
  43.     [Required(ErrorMessage = "Age is required")]    
  44.     [Range(typeof(int), "18""40", ErrorMessage = "Age can only be between 18 and 40")]    
  45.     public string age    
  46.     {    
  47.         get;    
  48.         set;    
  49.     }    
  50.   
  51. }   
Step 4: Now, let's add controller, Right click on controllers folder, click Add and then Controller.
 
 
 
Step 5: In your controller there is a default ActionResult 'Index'. Right click on that and click 'Add View':
 
 

It will open Add View dialog box, here select Template 'Create', so it will create view with all the fields.

From the Model Class select the ModelClass 'UserRegistration' which we have created in step 2. 

 
 
By default, the Create and Edit scaffolds now use the Html.EditorFor helper.

In the Index.cshtml View, it will generate page using razor syntax it has, 
  • @Html.EditorFor  : Bind the Model property an HTML Input element.
  • @Html.ValidationMessageFor : It is a html helper that display validation error message if model state is not valid.
  • @class = "text-danger" : Apply Validation class.  
 
 
Step 6: Don't forget to add jqueryval  in the footer part of our view. 
  1. @section Scripts {  
  2.     @Scripts.Render("~/bundles/jqueryval")  
  3. }  
That's it, now run your application and check all validations.
 
 
 
 
Its works fine for Required fields: Regular Expression for email and mobile, range validation, compare validation, etc.
 
Hope you like the simple steps to create Validation using data annotation in ASP.NET MVC.


Similar Articles