jQuery验证-使用相似的规则对输入进行分组 [英] jQuery validate - group inputs with similar rules

查看:72
本文介绍了jQuery验证-使用相似的规则对输入进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery validate来验证提交的表单.该表单具有三个输入字段,它们具有相同的规则,即仅数字和必填-true.我可以为所有三个输入写一条规则吗?-

I am using jquery validate, to validate a submitted form. The form has three input fields which have same rule i.e., only number and required - true. Can i write a single rule for all the three inputs say -

     rules: {
                "depository.startDate":{
                    required:true,
                    digits:true
                },
                "depository.endDate":{
                    required:true,
                    digits:true
                },
                "depository.port":{
                    required:true,
                    digits:true
                }
            },

 rules: {
            "depository.startDate, depository.port, depository.endDate ":{
                required:true,
                digits:true
            }
        },

我尝试了一下,但是没有用,以任何其他方式可以进行此验证工作.

I tried but its not working, any other way i can get this validation work.

- 谢谢

推荐答案

使用内置的rules()方法添加规则. 请参阅文档.

Use the built-in rules() method to add rules. See documentation.

注意:您必须必须在调用.validate()之后调用此方法,并且该方法必须与.each()组合.

Note: You must call this method after you call .validate(), and it must be combined with an .each().

jsFiddle演示

HTML:

 <input type="text" name="depository.startDate" />
 <input type="text" name="depository.endDate" />
 <input type="text" name="depository.port" />

jQuery:

$('#form').validate({
    // your other options
});

// the following method must come AFTER .validate()
$("input[name*='depository']").each(function() {
    $(this).rules('add', {
        required: true,
        digits: true
    });
});

在向表单中动态添加字段时,此方法也非常有用.

This method can also be very useful when you are dynamically adding fields to your form.

以下内容与自定义messages:结合使用.请注意,格式与在.validate() ...

The following to combine with custom messages:. Note that the format is slightly different than when adding rules as options within .validate()...

$("input[name*='depository']").each(function() {
    $(this).rules('add', {
        required: true,
        digits: true,
        messages: {
            required: "Required input",
            digits: "Only digits please"
        }
    });
});


或者,您可以改用class,但是要使其正常工作,还必须将其与.each() ...


Alternatively, you can use a class instead, but for it to work properly, you must also combine it with an .each()...

jsFiddle演示

HTML:

 <input type="text" class="myclass" name="depository.startDate" />
 <input type="text" class="myclass" name="depository.endDate" />
 <input type="text" class="myclass" name="depository.port" />

jQuery:

$('form').validate({
    // your other options
});

// the following method must come AFTER .validate()
$('form').find('.myclass').each(function() {
    $(this).rules('add', {
        required: true,
        digits: true
    });
});

这篇关于jQuery验证-使用相似的规则对输入进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆