jQuery Validate插件 - 如何创建简单的自定义规则? [英] jQuery Validate Plugin - How to create a simple custom rule?
本文介绍了jQuery Validate插件 - 如何创建简单的自定义规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用jQuery Validate插件创建简单的自定义规则(使用 不使用正则表达式的addMethod
)?
How do you create a simple, custom rule using the jQuery Validate plugin (using addMethod
) that doesn't use a regex?
例如,什么函数会创建一个仅在at时验证的规则选中一组复选框中的至少一个?
For example, what function would create a rule that validates only if at least one of a group of checkboxes is checked?
推荐答案
您可以通过执行以下操作来创建简单规则:
You can create a simple rule by doing something like this:
jQuery.validator.addMethod("greaterThanZero", function(value, element) {
return this.optional(element) || (parseFloat(value) > 0);
}, "* Amount must be greater than zero");
然后像这样应用:
$('validatorElement').validate({
rules : {
amount : { greaterThanZero : true }
}
});
只需更改'addMethod'的内容即可验证您的复选框。
Just change the contents of the 'addMethod' to validate your checkboxes.
这篇关于jQuery Validate插件 - 如何创建简单的自定义规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文