'The auto generated error message shows the date format as 'yyyyy/mm/dd' instead of 'mm/dd/yyyyy in ASP.Net MVC application
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.
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>
Solution 1:[1]
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))
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |