Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
477 views
in Technique[技术] by (71.8m points)

javascript - jQuery Validate Plugin-如何创建简单的自定义规则?(jQuery Validate Plugin - How to create a simple custom rule?)

How do you create a simple, custom rule using the jQuery Validate plugin (using addMethod ) that doesn't use a regex?(如何使用不使用正则表达式的jQuery Validate插件(使用addMethod )创建简单的自定义规则?)

For example, what function would create a rule that validates only if at least one of a group of checkboxes is checked?(例如,什么功能会创建仅在选中一组复选框中的至少一个时才验证的规则?)   ask by Edward translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"); And then applying this like so:(然后像这样应用:) $('validatorElement').validate({ rules : { amount : { greaterThanZero : true } } }); Just change the contents of the 'addMethod' to validate your checkboxes.(只需更改“ addMethod”的内容即可验证您的复选框。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...