Required error message not displaying for date Time field

Anjali Agarwal 1,366 Reputation points
2023-03-15T22:45:58.94+00:00

I have the following Model:

public partial class EmployeeInfo

{

public int EmployeeInfoId { get; set; }

 [DisplayName("DOB")]

[Required(ErrorMessage = "Birthdate is required. [MM/DD/YYYY]")]

public DateTime DateOfBirth { get; set; } 

}

this is what i have on my cshtml page:


 <div class="col-sm-4">
                        <label asp-for="DateOfBirth" class="control-label required" style="font-weight:bold;"></label>
                        <input class="form-control input-lg" required name=x size=10 maxlength=10 onkeydown="this.value=this.value.replace(/^(\d\d)(\d)$/g,'$1/$2').replace(/^(\d\d\/\d\d)(\d+)$/g,'$1/$2').replace(/[^\d\/]/g,'')" />
                        <span asp-validation-for="DateOfBirth" class="text-danger"></span>
                    </div>

<div class="form-group row">

I cannot put type="date" for this field. How can I see the validation error message for date of birth field since I am not putting type="date". I still want to see the error message saying "DOB is required", but I dont see any message.

Any help will be greatly appreciated.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,154 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,250 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,031 Reputation points
    2023-03-16T02:00:55.3766667+00:00

    Try this

    [Display(Name = "DueDate")]
    [Required(ErrorMessage = "DueDate is required.")]
    [DisplayFormat(ApplyFormatInEditMode = false, DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime? DateOfBirth { get; set; }	
    

    Example for a date in one of my code samples.

    D1A

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. AgaveJoe 26,191 Reputation points
    2023-03-16T00:27:38.8033333+00:00

    We can only guess how your code works without enough code to reproduce this issue.

    Does the controller validate if the ModelState is valid?

        if (!ModelState.IsValid)
        {
            return View(MyModel);
        }
    

    Reference documentation

    https://learn.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-7.0#model-state

    Does the view or layout reference the validation scripts?

    ASP.NET Core example

    @section Scripts {
        @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    }
    

    Have you checked if there are any JavaScript errors? What's with the RegEx? This post is tagged as ASP.NET MVC and ASP.NET Core. Which framework are you targeting?

    1 person found this answer helpful.
    0 comments No comments

  2. RomanMolozea22754 0 Reputation points
    2023-03-15T22:54:47.36+00:00

    public int EmployeeInfoId { get; set; }

    [DisplayName("DOB")]

    [Required(ErrorMessage = "Birthdate is required. [MM/DD/YYYY]")]

    public DateTime DateOfBirth { get; set; }

    0 comments No comments

  3. Rena Ni - MSFT 2,061 Reputation points
    2023-03-16T05:51:39.29+00:00

    Hi @Anjali Agarwal,

    You forget to use the asp-for for your input like below:

    <input class="form-control input-lg" required asp-for="DateOfBirth" size=10 maxlength=10 onkeydown="this.value=this.value.replace(/^(\d\d)(\d)$/g,'$1/$2').replace(/^(\d\d\/\d\d)(\d+)$/g,'$1/$2').replace(/[^\d\/]/g,'')" />
    
    

    Then be sure add the client side validation required js(jquery.validate.js and jquery.validate.unobtrusive.js) or judge the ModelState in the backend as server side validation like what other community member said.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,

    Rena