jonnyhitek
jonnyhitek

Reputation: 1551

jQuery validation plugin for two fields

I am using the Jquery Validation plug-in, however i need to add a "custom rule", i have 2 date fields and i need to ensure that the end date is not less than the start date. My problem is how to pass the two fields in as elements.

As i understand u set up a custom function something like this :

function customValidationMethod(value, element, params){  } 

But can't see how i could use it with two fields, if anyone has any ideas it would be greatly appreciated.

Upvotes: 0

Views: 321

Answers (1)

Nick Craver
Nick Craver

Reputation: 630379

The validation plugin docs provide a writeup for this, here are the relevant parts:

$.validator.addMethod("dateRange", function() {
  return new Date($("#fromDate").val()) < new Date($("#toDate").val());
}, "Please specify a correct date range, the first must be before the second.");

$("form").validate({
  //other rules, options, etc...
  groups: { dateRange: "fromDate toDate" } //show one error message, not two
});

Note that the custom method uses the IDs, the groups option uses the name attribute.

Upvotes: 2

Related Questions