views:

36

answers:

1

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.

A: 

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.

Nick Craver
cheers Nick - strange i had'nt ran into that article before thanks mate
jonathan p