In this article I will explain with an example, how to implement dd/MM/yyyy Date format validation for TextBox in ASP.Net Core Razor Pages.
The RegularExpression Data Annotation will be used along with RegularExpression (Regex) for enforcing dd/MM/yyyy Date format validation for TextBox in ASP.Net Core Razor Pages.
Note: For beginners in ASP.Net Core (.Net Core 7) Razor Pages, please refer my article ASP.Net Core 7 Razor Pages: Hello World Tutorial with Sample Program example.
 
 

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 RegularExpression Data Annotation attribute accepts the RegularExpression as first parameter. The RegularExpression will allow only dd/MM/yyyy date format.
The Required Data Annotation and the RegularExpression 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 { getset; }
}
 
 

Razor PageModel (Code-Behind)

The PageModel consists of following Handler method.

Handler method for handling GET operation

This Handler method left empty as it is not required.
public class IndexModel : PageModel
{
    public PersonModel Person { get; set; }
    public void OnGet()
    {
    }
}
 
 

Razor Page (HTML)

HTML Markup

The HTML of Razor Page consists of an HTML Form.
 

Implementing Validation

The Form consists of a Label element, a HTML INPUT TextBox, a SPAN element and a Submit Button.
The TextBox has 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 using Data Annotations and Model class is performed on Server Side.
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.
@page
@model Date_validation_Core_Razor.Pages.IndexModel
@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">
        <label asp-for="Person.BirthDate"></label>
        <input type="text" asp-for="Person.BirthDate" />
        <span asp-validation-for="Person.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

ASP.Net Core Razor Pages: Implement dd/MM/yyyy Date format validations
 
 

Downloads