In this article I will explain with an example, how to implement dd/MM/yyyy Date format validation for TextBox in ASP.Net Core MVC.
Regular Expression (Regex) to validate Date Format
Regular Expression (Regex)
(((0|1)[0-9]|2[0-9]|3[0-1])\/(0[1-9]|1[0-2])\/((19|20)\d\d))$
Model
The following Model class consists of one property BirthDate to which the following validation Data Annotation attributes have been applied.
1. Required Data Annotation attribute.
2. RegularExpression Data Annotation attribute.
The
Required Data Annotation and the
Regular Expression Data Annotation attributes have been specified with a property Error Message with a string value. As the name suggests, this string value will be displayed to the user when the respective validation fails.
public class PersonModel
{
[Display(Name = "Birth Date")]
[Required(ErrorMessage = "Birth Date is required.")]
[RegularExpression(@"(((0|1)[0-9]|2[0-9]|3[0-1])\/(0[1-9]|1[0-2])\/((19|20)\d\d))$", ErrorMessage = "Invalid date format.")]
public string BirthDate { get; set; }
}
Controller
The Controller consists of following Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
}
View
HTML Markup
Inside the View, in the very first line the PersonModel class is declared as Model for the View.
The View consists of an HTML Form which has been created using the Html.BeginForm method with the following parameters.
ActionName – Name of the Action. In this case the name is Index.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
Inside the View, the following three HTML Helper functions are used:-
1. Html.LabelFor – Displaying the Model property name.
2. Html.TextBoxFor – Creating a TextBox for the Model property.
3. Html.ValidationMessageFor – Displaying the Validation message for the property.
The Form also consists of a Submit button.
In order to enable Client-Side validations, you will need to inherit the following script files.
1. jquery.js
2. jquery.validate.js
3. jquery.validate.unobtrusive.js
Submitting the Form
When Submit button is clicked, the Form gets submitted.
@model Date_ddMMyyyy_validation_MVC.Models.PersonModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Index</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.21.0/jquery.validate.min.js;></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/4.0.0/jquery.validate.unobtrusive.min.js;></script>
</head>
<body>
@using (@Html.BeginForm("Index", "Home", FormMethod.Post))
{
@Html.LabelFor(m => m.BirthDate)
@Html.TextBoxFor(m => m.BirthDate)
@Html.ValidationMessageFor(m => m.BirthDate, "", new { @class = "error" })
<hr />
<input type="submit" value="Submit" />
}
</body>
</html>
Enabling Client-Side validations
By default, the validations performed using Data Annotations and Model class is performed on Server Side.
Then, adding the ClientValidationEnabled and UnobtrusiveJavaScriptEnabled keys in the AppSettings section in the Web.Config file.
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled"value="true" />
<add key="UnobtrusiveJavaScriptEnabled"value="true" />
</appSettings>
Screenshot
Downloads