使用JQuery Validate插件来验证具有相同名称的多个表单字段 [英] Using JQuery Validate Plugin to validate multiple form fields with identical names

查看:264
本文介绍了使用JQuery Validate插件来验证具有相同名称的多个表单字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态生成的表单,该表单的输入字段具有相同的名称(例如:"map").我不能选择更改字段名称或生成唯一的字段名称,因为表单处理程序代码(Perl/CGI)被设计为处理输入值的数组(在本例中为@map).

I have a dynamically generated form with input fields with the same name (for example: "map"). I do not have the option of changing the field names or generating unique field names because the form handler code (Perl/CGI) is designed to handle an array of input values (in this case @map).

如何使用JQuery 验证插件来验证表单这样的情况?具体来说,我希望提交的数组中的一个元素具有特定的固定值.我当前正在使用一个自定义事件处理程序,该事件处理程序使用serializeArray()创建一个JSON对象,然后遍历它以确保满足条件.但是,由于我在应用程序的其余部分中使用了Validate插件,因此我想知道是否也可以在此处使用相同的插件来处理这种情况.

How can I use the JQuery Validate Plugin to validate a form in such a situation? Specifically I would want exactly one element of the submitted array to have a certain fixed value. I am currently using a custom event handler that creates a JSON object with serializeArray() and then traverses it to ensure that the condition is met. But since I have used the Validate Plugin in the rest of the application, I was wondering if such a case may be handled using the same plugin here too.

感谢您的关注.

推荐答案

当我最终尝试了在多个字段上进行验证的最简单方法时,我花了一些时间搜索并尝试其他事情.每个字段及其克隆共享一个对于每个集合唯一的类.我只是遍历了该类的输入,并像往常一样添加了验证规则.我希望这可以帮助其他人.

I spent some time searching and trying different things when finally I tried the most trivial way of doing validation on multiple fields. Each field and it's clones share a class unique to each set. I just looped through the inputs with that class and added my validation rules as usual. I hope this might help someone else.

    $("#submit").click(function(){
    $("input.years").each(function(){
        $(this).rules("add", {
            required: true,
            messages: {
                required: "Specify the years you worked"
            }
        } );            
    });

    $("input.employerName").each(function(){
        $(this).rules("add", {
            required: true,
            messages: {
                required: "Specify the employer name"
            }
        } );            
    }); 

    $("input.employerPhone").each(function(){
        $(this).rules("add", {
            required: true,
            minlength: 10,
            messages: {
                required: "Specify the employer phone number",
                minlength: "Not long enough"
            }
        } );            
    }); 

    $("input.position").each(function(){
        $(this).rules("add", {
            required: true,
            messages: {
                required: "Specify your position"
            }
        } );            
    });             

    $("input.referenceName").each(function(){
        $(this).rules("add", {
            required: true,
            messages: {
                required: "Specify the reference name"
            }
        } );            
    });         

    $("input.referencePhone").each(function(){
        $(this).rules("add", {
            required: true,
            minlength: 10,
            messages: {
                required: "Specify your reference phone number",
                minlength: "Not long enough"
            }
        } );            
    });

// Now do your normal validation here, but don't assign rules/messages for the fields we just set them for





});

这篇关于使用JQuery Validate插件来验证具有相同名称的多个表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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