开发者

jQuery validation plugin for two fields

开发者 https://www.devze.com 2023-01-01 00:56 出处:网络
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 pas

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 custom开发者_运维知识库ValidationMethod(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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消