我正在使用优秀的jQuery Validate插件来验证某些表单.在一种形式上,我需要确保用户填写一组字段中的至少一个.我想我有一个很好的解决方案,想分享它. 请提出您可以想到的任何改进建议.
找不到这样做的内置方法,我搜索并找到了Rebecca Murphey的自定义验证方法,这非常有用.
我通过三种方式改进了这个:
所以你可以说"至少必须填充匹配选择器Y的X输入."
最终结果,标记如下:
<input class="productinfo" name="partnumber">
<input class="productinfo" name="description">
Run Code Online (Sandbox Code Playgroud)
......是一组这样的规则:
// Both these inputs input will validate if
// at least 1 input with class 'productinfo' is filled
partnumber: {
require_from_group: [1,".productinfo"]
}
description: {
require_from_group: [1,".productinfo"]
}
Run Code Online (Sandbox Code Playgroud)
项目#3假定您.checked
在成功验证后添加了一类错误消息.你可以这样做如下,这里展示.
success: function(label) {
label.html(" ").addClass("checked");
}
Run Code Online (Sandbox Code Playgroud)
正如在上面链接的演示中,我使用CSS给每个span.error
X图像作为其背景,除非它有类.checked
,在这种情况下它获得一个复选标记图像.
到目前为止,这是我的代码:
jQuery.validator.addMethod("require_from_group", function(value, element, options) {
var numberRequired = options[0];
var selector …
Run Code Online (Sandbox Code Playgroud)