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

前端之家收集整理的这篇文章主要介绍了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,作为不同的示例,但将有助于调试.

@H_403_48@

猜你在找的jQuery相关文章