[英]custom function in JQuery validation engine with arguments
I want to write a new validation rule for JQuery Validation Engine . 我想为JQuery Validation Engine编写一个新的验证规则。 It seems all you have to do is to write a function like this: 你似乎所要做的就是编写一个这样的函数:
"myNewRule":
"func": function(field, rules, i, options){
//new validation function
},
"alertText": "message to be displayed if something goes wrong"
},
Now, does anyone know how can I access the field id (or name) or how can I pass arguments? 现在,有谁知道如何访问字段ID(或名称)或如何传递参数? I need to verify that field B has a value no less than field A, so it's really important to know what field should I compare to... 我需要验证字段B的值是否小于字段A,因此知道我应该将哪个字段与...进行比较非常重要。
Okay, I figured it out. 好的,我明白了。
field.val()
to get the field value. 第一个参数(即字段)是一个字段对象,因此可以在其上调用jQuery的方法,例如,使用field.val()
来获取字段值。 i+1
gives you the position in the rules array where your rule starts, and it can be very useful if you have arguments. i+1
为您提供规则开始的规则数组中的位置,如果您有参数,它将非常有用。 I needed to verify that a field had a greater value than another, so I did the following: 我需要验证字段的值是否大于另一个字段,因此我执行了以下操作:
javascript: JavaScript的:
function geThan(field, rules, i, options){
var a=rules[i+2];
if(parseFloat(field.val()) < parseFloat( jQuery("#"+a).val() ) ){
return "Value is smaller than a, and should be greater than or equal to it."
}
}
and the html is: 和HTML是:
<input type="text" id="porce_1" name="porce_1" data-validation-engine="validate[required,custom[number],min[0],max[100]]">
<input type="text" id="porce_2" name="porce_2" data-validation-engine="validate[required,custom[number],min[0],max[100],funcCall[geThan[porce_1]]]">
I didn't place my code inside the plugin, as I had initially thought, but in the head section of my page and used funcCall
instead. 我没有将我的代码放在插件中,正如我最初想的那样,但是在我的页面的head部分中使用了funcCall
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.