1

I am using HTML5 date type inputs with min and max attributes set.

<input class="AccordionLeft" data-val="true" 
id="operationDate" max="2050-01-01" min="2014-09-02" 
name="OpDate" type="date" value="">

When I enter a date before the min date, I get the jquery validation message from the built-in validator. I would like to customize this. We have created several custom rules (via custom data annotations) and for our custrom validation rules, setting the error message was simple.

Is there anything like this for built-in validators?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Elad Lachmi
  • 10,406
  • 13
  • 71
  • 133
  • @mplungjan - I have tried setting the HTML5 validation, but it does no change the error message. Also, if I turn off jQuery validate, I don't get the error message any more, so it looks like the error message is coming from jQuery validate and not the built-in HTML5 input validation. – Elad Lachmi Aug 27 '14 at 10:55
  • Did you try looking for "jquery custom validation message" at all? http://stackoverflow.com/questions/6777634/jquery-validation-plugin-custom-message – mplungjan Aug 27 '14 at 11:29
  • @mplungjan - Yes I did, but since the rules are created by the unobtrusive validation script, I would rather not mess with it. – Elad Lachmi Aug 27 '14 at 11:31
  • @mplungjan - In any case, I found a solution that works for me below, so thank you for your time. – Elad Lachmi Aug 27 '14 at 11:33
  • @mplungjan - Yes, with that I agree. – Elad Lachmi Aug 27 '14 at 13:54

1 Answers1

6

After digging around a bit I found what I was looking for in the jQuery validate source code. In the full jQuery validate download there is a 'localization' folder. In this folder the default error messages are set for different languages. This is i.e. exactly what I am trying to do.

I added the following code to my custom JavaScript validations file:

$.extend($.validator.messages, {
    min: 'Min some message {0}',
    max: 'Max some message {0}'
});

And this replaces the default messages with the ones I need. The {0} bit is for formating. jQuery validate replaces this with the value in the Min / Max property.

Elad Lachmi
  • 10,406
  • 13
  • 71
  • 133