jQuery使用动态数量的字段进行验证 [英] jQuery validate with a dynamic number of fields

查看:139
本文介绍了jQuery使用动态数量的字段进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是一个具有动态数量的字段组合的表格,其中的数字基于前一阶段的答案。生成字段服务器端作为数组,即

 < input id =foo [0]... 
< input id =bar [0]...

< input id =foo [1]...
< input id =bar [1]...

< input id =foo [2]...
< input id =bar [2]... etc

无论数字,所有字段都是必需的&我也需要验证类型&在某些情况下的位数。我正在使用 jQuery验证插件进行客户端处理(是的,使用server-侧面的东西)&验证不能内联,因为表单需要通过XHTML Strict(编辑:见我的附录)。



我的问题是我无法解决如何使用验证与动态数量的字段。以下是其他表单的验证语法:

  $(document).ready(function() 

//在keyup上验证stage_form并提交
var validator = $(#form_id)。validate({

//字段名称的规则
规则:{

name:required,
address:required,
age:{required:true,number:true}

$,

//
以外的字段的内联错误消息:{

名称:请输入您的姓名,
地址: 请输入您的地址,
年龄:{必填:请输入您的年龄,号码:请输入一个数字}

}

} );

});


解决方案

实际上应该工作如果你要使用类将规则初始化为validate()选项。



标记:

 < input id =foo [0]class =required
< input id =bar [0]class =required number

< input id =foo [1]class =required
< input id =bar [1]class =required email

jQuery:

  $(document).ready(function(){

var validator = $(#form_id)。validate({
messages:{
name:请输入您的姓名,
地址:请输入您的地址,
年龄:{
必填:请输入您的年龄,
号码:请输入一个数字
}

}

});

});

希望这有用。
Sinan。


I have a form with a stage that has a dynamic number of groups of fields, where the number is based upon answers in the previous stage.

I'm generating the fields server-side as an array, i.e.

<input id="foo[0]"...
<input id="bar[0]"...

<input id="foo[1]"...
<input id="bar[1]"...

<input id="foo[2]"...
<input id="bar[2]"... etc

No matter the number, all fields are required & I also need to validate against type & number of digits in some cases. I'm using the jQuery validate plugin for client-side processing (yes, backed up with server-side stuff too) & the validation can't be done inline as the form needs to pass XHTML Strict (EDIT: see my addendum below).

My problem is that I can't work out how to use validate with a dynamic number of fields. Here's what the validate syntax typically looks like for the rest of the form:

$(document).ready(function() {

    // validate stage_form on keyup and submit
    var validator = $("#form_id").validate({

        // rules for field names
        rules: {

            name: "required", 
            address: "required",
            age: { required: true, number: true }

        },

        // inline error messages for fields above
        messages: {

            name: "Please enter your name", 
            address: "Please enter your address",
            age: { required: "Please enter your age", number: "Please enter a number" }

        }

    });

});

解决方案

actually it should work If you'd use classes instead of initializing rules as validate() options.

Markup:

<input id="foo[0]" class="required"
<input id="bar[0]" class="required number"

<input id="foo[1]" class="required"
<input id="bar[1]" class="required email"

jQuery:

$(document).ready(function() {

  var validator = $("#form_id").validate({
    messages: {
            name: "Please enter your name", 
            address: "Please enter your address",
            age: { 
               required: "Please enter your age", 
               number: "Please enter a number" 
            }

    }

  });

});

hope this works. Sinan.

这篇关于jQuery使用动态数量的字段进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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