Validating multiple “array named” file inputs and dropdowns in JQuery Validate

前端 未结 1 502
陌清茗
陌清茗 2020-12-22 04:22

I have search lot of about this but haven\'t found any solution. I want to validate multiple array named file inputs and dropdowns in JQuery validate.



        
相关标签:
1条回答
  • 2020-12-22 04:45

    The plugin doesn't handle fields with the same name well. Here's how I solved it in my application.

    I gave all the repetitive fields distinct names, and put the validation method names in the class.

    <td> <input type="text" class="times required digits" name="times[0]" /> </td>
    </tr>
    <tr>
    <td> <input type="text" class="times required digits" name="times[1]" /> </td>
    </tr>
    <tr>
    <td> <input type="text" class="times required digits" name="times[2]" /> </td>
    </tr>
    

    You can remove the indexes before submitting with code like this:

    $("#formname").validate({
        ...
        submitHandler: function(form) {
            $(form).find(":input[name*='[']").each(function() {
                this.name = this.name.replace(/\[\d+\]/, '[]');
            }
            form.submit();
        }
    });
    
    0 讨论(0)
提交回复
热议问题