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

Sergejs
  • 2,540
  • 6
  • 32
  • 51
  • Check this: http://stackoverflow.com/questions/2176663/how-to-replace-standard-dataannotations-error-messages – danywalls Jan 18 '13 at 10:46
  • If you want to validate that the value that is being entered by the user in the corresponding input field you will have to write a custom model binder.http://stackoverflow.com/questions/6921928/displayformat-dataannotation-not-working – robasta Jan 18 '13 at 10:47

1 Answers1

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")
NinjaNye
  • 7,046
  • 1
  • 32
  • 46
  • getting the same... not even rendered, any ideas why? – Sergejs Jan 18 '13 at 11:35
  • You still need to use `@Html.TextBoxFor(x => x.OwnerBirthDate)`. The above is in addition for a custom error. – NinjaNye Jan 18 '13 at 11:37
  • 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 – Sergejs Jan 18 '13 at 11:39
  • Adding code:
    @Html.ValidationMessageFor(m => m.OwnerBirthDate, custom error message")
    In genrated output(in browser) i can see empty
    – Sergejs Jan 18 '13 at 12:34