验证多个“名为”的数组JQuery Validate中的文件输入和下拉列表 [英] Validating multiple "array named" file inputs and dropdowns in JQuery Validate

查看:67
本文介绍了验证多个“名为”的数组JQuery Validate中的文件输入和下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了很多这方面但没有找到任何解决方案。我想在JQuery验证中验证多个数组命名文件输入和下拉列表。

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.

<td> <input type="text" class="times" name="times[]" /> </td>
</tr>
<tr>
<td> <input type="text" class="times" name="times[]" /> </td>
</tr>
<tr>
<td> <input type="text" class="times" name="times[]" /> </td>
</tr>

我已经添加了这样的JQuery Validate Code:

I have added JQuery Validate Code like this:

$("#formname").validate(
    {
    rules:{
        times:{required:true, digits:true}
    }}
    );

但它唯一有效的第一个输入并显示错误信息,只是输入了第二个或第三个输入字段。

But its only validating first input and showing error message there only whether 2nd or 3rd input field entered or not.

我不想更改这个times []名称,因为功能取决于此。只有我想要使用JQuery验证验证。

I don't want to change this "times[]" name because functionality is depending on this. Only I want validation using JQuery validate.

有没有可用的技巧?

任何帮助都是赞赏。

谢谢

推荐答案

该插件不处理字段同名的。以下是我在我的应用程序中解决它的方法。

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();
    }
});

这篇关于验证多个“名为”的数组JQuery Validate中的文件输入和下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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