jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了javascript – 如何使用jQuery Validator比较两个字段大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想通过比较一个来验证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,100]
            },endPoint: {
                required: true,range: [1,100],endGreaterThanBegin: true
            },}
    });
    if ($(theForm).valid()) {
        return true;
    }
    else {
        return false;
    }
}

在我开始构建之前获取$.validator是未定义的,然后当我开始测试输入字段时,validateSection不是一个函数

解决方法

需要添加一个 custom validate method

原型 –

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

验证 –

endate_greater_startdate : true

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

大佬总结

以上是大佬教程为你收集整理的javascript – 如何使用jQuery Validator比较两个字段全部内容,希望文章能够帮你解决javascript – 如何使用jQuery Validator比较两个字段所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。