In this article I will explain with an example, how to implement Date Format server side dd/MM/yyyy Date Format validation in ASP.Net Core (.Net Core 8) Razor Pages.
The Date Format validations will be performed using Regular Expression (Regex) in ASP.Net Core (.Net Core 8) Razor Pages.
 
 

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 { get; set; }
}
 
 

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.
 

Handler method for handling POST operation

This Handler method handles the POST operation and when the form is submitted, the object of the PersonModel class is sent to this method.
The state of the submitted Model is checked using ModelState.IsValid property.
public class IndexModel : PageModel
{
    public PersonModel  Person{ get;set; }
    public void OnGet()
    {
    }
 
    public void OnPostSubmit(PersonModel person)
    {
        if (ModelState.IsValid)
        {
            //Validation Success.
        }
    }
}
 
 

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.
@page
@model Date_ddMMyyyy_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" style="color:red"></span>
        <hr />
        <input type="submit" value="Validate" asp-page-handler="Submit" />
    </form>
</body>
</html>
 
 

Screenshot

ASP.Net Core Razor Pages: Server Side dd/MM/yyyy Date Format validation
 
 

Downloads