1

I have a DateTime field(that can accept multiple date time formats, so it is pain to create Regex patter)

When in the field I'm entering something like "Aaaaaa", I'm getting error message:

The value 'Aaaaa' is not valid for OwnerBirthDate

Model looks:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy}")]
[Required(ErrorMessage = "*")]
public DateTime? OwnerBirthDate { get; set; }

View:

@Html.TextBoxFor(x => x.OwnerBirthDate)

How can I define custom error message for this particular field?

Thank you

2

1 Answer 1

2

You can use ValidationMessageFor to apply a custom message if you want a different message to one pre defined in an attribute

@Html.ValidationMessageFor(m => m.OwnerBirthDate, "custom error message")
Sign up to request clarification or add additional context in comments.

4 Comments

getting the same... not even rendered, any ideas why?
You still need to use @Html.TextBoxFor(x => x.OwnerBirthDate). The above is in addition for a custom error.
I do, but in controller in model state I'm seeing old error message. @Html.ValidationMessageFor(m => m.OwnerBirthDate, "custom error message") - bot even rendered
Adding code: <div id = "test"> @Html.ValidationMessageFor(m => m.OwnerBirthDate, custom error message") </div> In genrated output(in browser) i can see empty <div id="test">

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.