我想通过比较两个字段来验证2个输入字段,并确保第二个字段大于第一个字段。如何使用jQuery验证器来比较两个字段
我需要添加一个自定义的方法吗?或者我可以在范围方法中使用变量名吗?如果是这样,你能指出我的语法吗?
var validateSection = function (theForm) {
$(theForm).validate({
rules: {
startPoint: {
required: true,
range: [0, 100]
},
endPoint: {
required: true,
range: [startPoint + 1, 100] //Is this possible if I set the function to run on any change to either field?
},
}
});
if ($(theForm).valid()) {
return true;
}
else {
return false;
}
}
代码自定义方法:之前,我形成
$.validator.addMethod("endGreaterThanBegin", function(value, element) {
return endPoint > startPoint
}, "* End Point Should be Greater than Start");
var validateSection = function (theForm) {
$(theForm).validate({
rules: {
startPoint: {
required: true,
range: [0, 100]
},
endPoint: {
required: true,
range: [1, 100],
endGreaterThanBegin: true
},
}
});
if ($(theForm).valid()) {
return true;
}
else {
return false;
}
}
获得$ .validator是不确定的,然后validateSection不是一个函数,当我开始测试输入字段
添加的代码试图自定义方法的建议 – jimdrang