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. Display Data Annotation attribute.
2. Required Data Annotation attribute.
3. 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 method.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
public class HomeController : Controller
{
public IActionResult 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 Form
The View consists of an HTML Form which has been created using the Html.BeginForm method with the following parameters.
asp-action – Name of the Action. In this case the name is Index.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
Implementing Validation
The Form consists of a Label element, an HTML INPUT TextBox, a SPAN element and a Submit Button.
The TextBox have been set with the following Tag Helpers attributes:-
asp-for – The Model property to which validation will be performed. In this case BirthDate.
asp-validation-for – Displaying the validation message for the Model property.
When the Submit button is clicked, the Form gets submitted.
Enabling Client-Side validations
By default, the validations performed on Server Side using Data Annotations attributes.
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
Once, the above files are inherited automatically, the Client-Side validations using Data Annotations is enabled.
@model Date_ddMMyyyy_validation_Core.Models.PersonModel
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form method="post" asp-action="Index" asp-controller="Home">
<label asp-for="BirthDate"></label>
<input type="text" asp-for="BirthDate"/>
<span asp-validation-for="BirthDate" class="error"></span>
<hr />
<input type="submit" value="Submit" />
</form>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/jquery.validate.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validation-unobtrusive/3.2.12/jquery.validate.unobtrusive.js"></script>
</body>
</html>
Screenshot
Downloads