Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show a custom error message for DisplayFormat attribute In Asp.Net MVC?

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.

like image 653
Diganta Kumar Avatar asked Nov 04 '22 07:11

Diganta Kumar


1 Answers

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

like image 166
Grey Wolf Avatar answered Nov 07 '22 20:11

Grey Wolf