5

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.

2
  • 1
    What do you have for DocumentIssueDate_DataType? Commented Jan 25, 2012 at 4:00
  • A string message in the resource file (.resx). For e.g. "Date must be entered as dd/mm/yyyy (e.g. 31/01/1997)." Commented Jan 27, 2012 at 23:24

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

Try see answers Here. I think DisplayFormat designed not for validation, that is why you can not use it to check if entered string is valid.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.