gpt4 book ai didi

javascript - 如何使用 jQuery Validator 比较两个字段

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:57:41 25 4
gpt4 key购买 nike

我想通过将一个与另一个进行比较来验证 2 个输入字段,并确保第二个大于第一个。

我需要添加自定义方法还是可以只在范围方法中使用变量名?如果是这样,你能指出语法吗?

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 is undefined before I get to form,然后validateSection is not a function when I start testing input fields

最佳答案

需要加一个custom validate method

原型(prototype) -

$.validator.addMethod("endate_greater_startdate", function(value, element) {
return enddate > startdate
}, "* Enddate should be greater than Startdate");

验证 -

endate_greater_startdate : true

检查 Demo ,作为不同的示例,但将有助于调试。

关于javascript - 如何使用 jQuery Validator 比较两个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8090485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com