home / software / tips and tricks / How to create a custom ValidationAttribute in MVC 3?

How to create a custom ValidationAttribute in MVC 3?

Updated:  09/18/2013 11:09 AM
Author:  Shiju Mathews

Status:    Resolved.


I am creating a custom attribute to compare StartDate and EndDate on a model and make sure that EndDate is not earlier than StartDate.

Step 1.
Add a class to the project in Validation folder named GraterThanDateAttribute, that is inherited from ValidationAttribute.


Step 2.
create a model(class) named TimeCard in the model folder(or any sub filder, I created a folder called "TimeSheet" under Model folder).

Step 3.

Add a reference to validation name space(SoftwareTalk.Validation) to the Model class

Step 4.
Add the Properties to the model class.


Now we are ready to use the model "TimeCard" any where in the MVC application.
custom ValidationAttribute in MVC


Now you can extend the functionality by adding client side custom ValidationAttribute  How to create a client side custom ValidationAttribute in MVC 3

Tags: How to create a custom ValidationAttribute in MVC 3?
Updated on: October 2024