Custom Date validation of date format and other form attributes in ASP.NET MVC


Custom validator in ASP.NET MVC to check dateformat (dd/mm/yyyy) and check this value with other form attribute

This atrticle will help you to understand how to write custom validator in ASP.NET MVC and how to check a value of a particular field with another field in the form in the custom validation code. These validations happen in the server side so irrespective of any client validation application should have validation at server side. Rule of thumb is All user inputs are BAD unless validated. So validate at client side, at app server level and then in database server (SQL server / Oracle stored procedures)


You should have basic understanding of how MVC application works though i have included steps on how to create your first MVC application but this article is for those users who are aware of MVC. This code is written and tested in VS 2010 .NET framework 4.0 but logic should remain same more or less with higher versions.

Using the code

Fiirst lets start with creating our MVC application

1. Open Visual studio. Go to File -> New Project -> Other Languages -> Web -> C#  Select ASP.NET MVC 4 Application

2. After you click on OK you will get another window. Select Basic and Click on OK

3. Now your MVC application is created. Now we will create our Model class. This class will be similar to what we want in our HTML form from the user. Right Click on Models folder and go to Add-> Class. Create a new class Employee.cs.

Structure of Employee class should be like this

  public class Employee
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public string Department { get; set; }
        public int PhoneNumber { get; set; }
        public string DateOfBirth { get; set; }
        public string JoiningDate { get; set; }

We will take inputs from user for all the above mentioned properties of this class. We will come onto that later in this tutorial. Compile your project at this stage otherwise step 3 will not work.

3. Now we will add our View and map it to this Employee Class. Right click on Views folder and Goto Add -> New Folder. Create a Folder Home under Views folder. Now right click on Home folder and goto Add -> View

Name the view as index and make the settings as shown in below screenshot. Remember to Click on “Create a strongly types view” and from drop down select Employee class. If you do not see anything in the drop down build your project.

4. Now open your view and make the changes as below

@model MvcApplicationDataValidation.Models.Employee

    ViewBag.Title = "index";
    Layout = "~/Views/Shared/_Layout.cshtml";

@{ using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    <tr><td>Employee Id</td><td>@Html.TextBoxFor(m => m.EmployeeId)</td><td>@Html.ValidationMessageFor(m => m.EmployeeId)</td></tr>
    <tr><td>Employee Name</td><td>@Html.TextBoxFor(m => m.EmployeeName)</td><td>@Html.ValidationMessageFor(m => m.EmployeeName)</td></tr>
    <tr><td>Department</td><td>@Html.TextBoxFor(m => m.Department)</td><td>@Html.ValidationMessageFor(m => m.Department)</td></tr>
    <tr><td>Phone Number</td><td>@Html.TextBoxFor(m => m.PhoneNumber)</td><td>@Html.ValidationMessageFor(m => m.PhoneNumber)</td></tr>
    <tr><td>Date Of Birth (dd/mm/yyyy)</td><td>@Html.TextBoxFor(m => m.DateOfBirth)</td><td>@Html.ValidationMessageFor(m => m.DateOfBirth)</td></tr>
    <tr><td>Joining Date (dd/mm/yyyy)</td><td>@Html.TextBoxFor(m => m.JoiningDate)</td><td>@Html.ValidationMessageFor(m => m.JoiningDate)</td></tr>
    <tr><td></td><td><input type="submit" value="Submit"/></td></tr>

HTML of this view is like this:

Notice that all fields in the View are mapped to the properties of the Model.

Now our basic structure of the MVC application We will add validations for the input fields of this form

There is an inbuilt functionality in MVC where we have some predefined validations for these fields.

Validations are added as an attribute on the properties of Model class (Employee) and code looks like this

 [StringLength(50, MinimumLength = 3)]
 public string Department { get; set; }

Here the Required attribute will make this field mandatory and StringLength attribute will make the value of Department maximum value 50 and min length of 3. If you click on Submit button you should see the error message if you keep Department textbox empty or minimum length less than 3 or maximum value greater than 50. See the screenshot:

So far we have implemented inbuilt MVC validation attribute. Now we will create our own Custom Validation.

We will validate the date format of “Date of Birth” and “Joining Date”. We are expecting format in dd/mm/yyyy. We will also validate that Joining Date should be greater than Date of Birth and we will also validate that Date of Birth should be less than current date. Here you will get an idea of how to write custom validators. Once you understand writing custom validators you can perform any kind of validation in MVC by writing custom validations.

5. Add a new folder Validators in the project. We will keep our custom validation class in this folder.

6. Add a new class in this Folder and name it DateValidatorAttribute and add the below code:

public class DateValidatorAttribute : ValidationAttribute
 protected override ValidationResult IsValid(object value, ValidationContext validationContext)
 string strValue = value.ToString();
 var dateTime = DateTime.Parse(strValue);
 string pattern = @"(^(((0[1-9]|1[0-9]|2[0-8])[\/](0[1-9]|1[012]))|((29|30|31)[\/](0[13578]|1[02]))|((29|30)[\/](0[4,6,9]|11)))[\/](19|[2-9][0-9])\d\d$)|(^29[\/]02[\/](19|[2-9][0-9])(00|04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)$)";
 Match match = Regex.Match(strValue, pattern);
 if (match.Success)
 var property = validationContext.ObjectType.GetProperty("JoiningDate");
 if(property == null)
 return new ValidationResult(string.Format("Property '{0}' is Null", "JoiningDate"));
 var objJoiningDate = property.GetValue(validationContext.ObjectInstance,null);
 string strJoiningDate = objJoiningDate == null ? "" : objJoiningDate.ToString();
 Match matchJoiningDate = Regex.Match(strJoiningDate, pattern);
 if (matchJoiningDate.Success)
 var joiningDate = DateTime.Parse(strJoiningDate);
 if(dateTime > joiningDate)
 return new ValidationResult("Date of Birth can not be greater than Joining date");
 if (dateTime > DateTime.Now)
 return new ValidationResult("Date of Birth can not be greater todays date");
 return new ValidationResult("Invalid date format");
 return ValidationResult.Success;
 catch (Exception)
 return new ValidationResult("Invalid date format");

We should inherit the .NET ValidationAttribute class so that we can write our custom logic for validation. Now we will override the IsValid method of ValidationAttribute class. 

object value contains the text of the html field. This html field value has already been mapped to the property of the Employee class. 

Here first we are converting the value into string and parsing it to DateTime.parse function to check that the values are valid irrespective of the format. Now we have used regular expressions to validate that the string is in the format dd/mm/yyyy. You can modify the regex to include other formats.

So far we have validated that the date is valid and is in the correct format. Next we will validate that this value is not greater than Joining Date and also it is not greater than current date.

var property = validationContext.ObjectType.GetProperty("JoiningDate");

The above code get the property of the JoiningDate property of the class Employee. Now we will get the value of this property (This has been entered by the user in the View in Joining Date textbox)

var objJoiningDate = property.GetValue(validationContext.ObjectInstance,null);

Now we have the object of the value and we will convert it to string and do the Regex again to match the pattern. We will check that Joining Date is also in the correct format dd/mm/yyyy. Once all validations are passed we will convert them to DateTime format and do the validation

var joiningDate = DateTime.Parse(strJoiningDate); if(dateTime > joiningDate) return new ValidationResult("Date of Birth can not be greater than Joining date"); if (dateTime > DateTime.Now) return new ValidationResult("Date of Birth can not be greater todays date");

This is just an example of how to get values of other fields of HTML form in the custom validation. This way you can perform all kind of validations by writing your own code. Also this article might help you validate the date time format if your application is taking input from user rather than from Calendar control.


This is first version