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

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

问题描述

我有一个动态生成的表单,其中包含同名的输入字段(例如:地图").我没有更改字段名称或生成唯一字段名称的选项,因为表单处理程序代码 (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 Validate Plugin 来验证表单这样的情况?具体来说,我希望提交的数组中只有一个元素具有某个固定值.我目前正在使用一个自定义事件处理程序,它使用 serializeArray() 创建一个 JSON 对象,然后遍历它以确保满足条件.但是由于我在应用程序的其余部分使用了 Validate Plugin,我想知道是否可以在这里使用相同的插件处理这种情况.

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 Plugin 验证多个具有相同名称的表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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