How to create custom validation attribute in mvc 4

How to create custom validation attribute in mvc 4

Introduction:

In this tutorial i am going to explain about How to create custom validation attribute in mvc 4. In my previous tutorial i have explained about ASP.NET MVC client side validation using data annotationsASP.Net Web Api Model Validation Using Validation FiltersHow To Create Help Pages(Documents) For Asp.Net Web APIHow to get/fetch data from ASP.Net WebAPI or how to query ASP.Net WebAPI. and How to Add Store Custom Objects in Configuration File(web.config & app.config) in ASP.Net & C#.Net

Implementation of how to create custom validation attribute in mvc 4:

Now we will see the implementation of How to create custom validation attribute in mvc 4. Before proceeding please check the below tutorials to understand how to do crud operations in asp.net mvc and entity framework and how to use data annotations for validating model.

  1. CRUD operations using Entity Framework Database First Approach and MVC 4
  2. CRUD operations using Entity Framework Code First Approach and MVC 4
  3. ASP.NET MVC client side validation using data annotations

Step 1 :

Create a new project and inside model folder add new class file and name it as CustomEmailValidator.cs. For creating Custom validation attributes a class should inherit from ValidationAttribute base class and should override IsValid method.

If the validation rule passes then it will return the result as ValidationResult.Success else it will return new ValidationResult(“Error message here”). Below is the entire code of CustomEmailValidator.cs which i used to validate the emailid.

Please remember to create the custom validation attribute you need to import the System.ComponentModel.DataAnnotations namespace inside your class.

Step 2:

Now add Customer model inside model folder and add the CustomEmailValidator attribute to CustomerEmail attribute. Below is the customer model.

Step 3:

Now add a controller inside controllers folder and name it as CustomerController.cs and add a action method for Create action. The final code will look like this.

Step 4:

Now right click on the create action method and choose Add View option for adding view to the customer create action. Choose Create template as view template and Customer model as model class for template and click add. The generated code is given below.

Output:

Now if you run the program you can see the output of how to create custom validation attribute in mvc 4. First you will see the Customer Email is required error message since the field is empty like shown in below output.

Output 1 of how to create custom validation attribute in mvc 4

Now if you enter some random string in email field and click Create button then it will through the error message as Please enter a valid email as defined in CustomEmailValidator.cs like shown below.

Output 2 of how to create custom validation attribute in mvc 4

If you enter the valid email and submit the form then only it will be submitted.

Conclusion:

Hope you learned how to create custom validation attribute in mvc 4.  Do you like this tutorial? Help us to improve. Please post your comments and feedback below.

Leave a Reply

Your email address will not be published. Required fields are marked *