0

I need to set dynamic validation message for a custom date range validator for input from datepicker.

I have created a validator method like -

$.validator.addMethod("minMaxDatevalidation", function (value, element) { 
...})

And created a custom rule like -

$(element).rules("add", {
    minMaxDatevalidation: true,
    messages: { minMaxDatevalidation: "The " + fieldDisplayName + " field 
    must be on or after the " + moment(pastDate).format("DD/MM/YYYY") + "."
    }
});

But this validation messages in the above add rules section - this I want dynamic - for date selected than a particular year then above or if selected date is more than a particular year than future error message.

I tried to return custom message from validator addMethod, but it's not working.

Any help regarding setting dynamic message in the rules section would be great.

Using Jquery datepicker.

10
  • What error do you get? And which validation plugin are you using? Commented Oct 12, 2017 at 7:41
  • I just need to display a dynamic message. I'm using jquery. Commented Oct 12, 2017 at 7:42
  • $.validator.addMethod methods are not jQuery's, these are usually provided by validator plugins. Please check your page's source code to see if there is any other validation library included other than jQuery. Commented Oct 12, 2017 at 7:44
  • Yes it is, jquery validation plugin only. We're also using foolproof, but extending jquery for validations. Commented Oct 12, 2017 at 7:55
  • You can define the custom message when you are creating the new rule using addMethod. This method takes a third parameter, which can be a function returning the message string. Check this answer for more details: stackoverflow.com/questions/13352626/… Commented Oct 12, 2017 at 8:07

0

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.