How to show a custom error message for
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
The code in my model is following,
[Display(Name = "When was that document issued?")]
[DataType(DataType.Date, ErrorMessageResourceName = "DocumentIssueDate_DataType",ErrorMessageResourceType = typeof(CustomErrorMessages))]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
public DateTime? DocumentIssueDate { get; set; }
In my View when I enter 201 in the date textbox I get the following error message. How do I modify the error message below.
The value '201' is not valid for DocumentIssueDate.
After a lot of research I had no luck. The only solution was to use RegularExpression
.
Instead of using:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
You can use:
[RegularExpression(@"^(3[01]|[12][0-9]|0[1-9])[-/](1[0-2]|0[1-9])[-/][0-9]{4}$", ErrorMessageResourceName = "Date Not Valid")]
The RegularExpression
matches the date format dd/MM/yyyy
.
regular still not test yes but you can search and edit if need
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With