我希望通过比较两个输入字段来验证它们,并确保第二个输入字段大于第一个输入字段。
我是否需要添加自定义方法,还是只能在范围方法中使用变量名称?如果是这样,你能指点我的语法吗?
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不是函数
答案 0 :(得分:12)
原型 -
$.validator.addMethod("endate_greater_startdate", function(value, element) {
return enddate > startdate
}, "* Enddate should be greater than Startdate");
验证 -
endate_greater_startdate : true
检查Demo,作为不同的示例,但有助于调试。
答案 1 :(得分:1)
的Javascript
NSWindowsCP1252StringEncoding , NSISOLatin1StringEncoding , NSUTF8StringEncoding
HTML
$(function() {
$.validator.addMethod('smaller_than', function (value, element, param) {
return +$(element).val() < +$(param).val();
});
$.validator.addMethod('not_smaller_than', function (value, element, param) {
return +$(element).val() > +$(param).val();
});
$('form').validate();
});