0

I am working on ASP.Net MVC 4.7.2 app. It has a date field where user can select or type the date. If user enter the date with year yyyyy (the length of year is greater than 4 here)then the error shows up which show the date in 'yyyy/mm/dd' format instead of 'mm/dd/yyyy' format. See the screen shot below. I want to show the same format in the error message as mm/dd/yyyy'. To fix the issue I did search and it seemed this is generated from kendu whereas I am not using the kendu control for date field. As a work around, I thought I can limit the max length but that did not work as well.

enter image description here

View html:

 <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-xs-2" })
            <div class="col-xs-8">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>
Jashvita
  • 553
  • 3
  • 24
  • https://stackoverflow.com/questions/15377131/customize-the-error-message-mvc-for-an-invalid-datetime-in-asp-net-mvc4 Maybe this is good for you. – Duc Nguyen May 12 '22 at 10:30

1 Answers1

0

I try some research and seem like it's hard to change the error message with the right format. You can update the error message to "Please enter a valid date" or "Please enter a valid date with format mm/dd/yyyy"

            @Html.EditorFor(model => model.Name, new
        {
            htmlAttributes = new { @class = "form-control" },
            data_val_date = String.Format("Please enter a valid date",
            Html.DisplayNameFor(model => model.Name))
        })
Duc Nguyen
  • 530
  • 8
  • 16
  • Thanks for your help if I update the validationMessage as you suggested then the message appears even before the submission of the view. The expectation is the error should show when the view is submitted not when user get to the form. Do you know how I can show that message only when it is missing the required filed or when date error occured? – Jashvita May 12 '22 at 17:14
  • https://stackoverflow.com/questions/15377131/customize-the-error-message-mvc-for-an-invalid-datetime-in-asp-net-mvc4 the similar question. – Duc Nguyen May 13 '22 at 07:08