Jquery 验证器与二维数组

发布于 2024-11-29 08:07:12 字数 497 浏览 0 评论 0原文

我将从我的问题的示例开始:

<form>
    <input name="course[0][name]" />
    <input name="course[0][type]" />

    <input name="course[1][name]" />
    <input name="course[1][type]" />

    ...
    ...
</form>

obs:这当然是简化的。

那么...既然我无法预测名字,我该如何验证这些呢?有没有办法使用正则表达式之类的?乌托邦示例:

rules: {
    /course\[([0-9]+)\]\[name\]/: {
        required: true
    }
}

我在文档中找不到解决方案,因为它有点令人困惑。谢谢 (:

I'll just start with the example of my problem:

<form>
    <input name="course[0][name]" />
    <input name="course[0][type]" />

    <input name="course[1][name]" />
    <input name="course[1][type]" />

    ...
    ...
</form>

obs: this is, of course, simplified.

So...how can i validate those since i can't predict the name? Is there a way to use regular expressions or something? utopic example:

rules: {
    /course\[([0-9]+)\]\[name\]/: {
        required: true
    }
}

I couldn't find the solution in the documentation since its a bit confusing. thank you (:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽清梦 2024-12-06 08:07:12

作为替代方案,你可以做这样的事情

$.validator.addClassRules({
    inputName: {
        required: true,
        minLength: 5
    },
    inputType: {
        required: true,
        remote: "remoteurl"
    }
});
<input name="course[0][name]" class="inputName"/>
<input name="course[0][type]" class="inputType"/>

As an alternative you can do something like this

$.validator.addClassRules({
    inputName: {
        required: true,
        minLength: 5
    },
    inputType: {
        required: true,
        remote: "remoteurl"
    }
});
<input name="course[0][name]" class="inputName"/>
<input name="course[0][type]" class="inputType"/>
青衫负雪 2024-12-06 08:07:12

所以,我做了一些搜索,看起来规则对象中的键是简单的 jQuery 选择器。根据这个问题,有一个可以使用的正则表达式过滤器。在两者之间,上面的正则表达式应该可以工作。


最初的想法:

......你不需要预测它们,你的服务器已经知道它们。只需将该数字输出到脚本标记中,然后循环即可。哎呀,您甚至可以为您提供服务器循环。

如果这不是一个选项,那么您可以使用 jQuery:

var rules = {
 // simple rule, converted to {required:true}
 name: "required",
 // compound rule
 email: {
   required: true,
   email: true
 }
 // whatever
}
// obviously, for just required, there are easier ways
var checkRules = {required:true}; 
$('#form-id input').each(function(i, elem)
{
   /check\[\d\]\[(name|type)\]/.test(elem.name)
   rules[elem.name] = checkRules
});

So, I did a little bit of searching, and it looks like the keys in the rules object are simple jQuery selectors. According to this question, there is a regex filter which can be used. Between the two, your regex above should work.


Original idea:

... you don't need to predict them, your server already knows them. Just output that number into a script tag and then loop. Heck, you can even have the server loop for you.

If that is not an option, then you can use jQuery:

var rules = {
 // simple rule, converted to {required:true}
 name: "required",
 // compound rule
 email: {
   required: true,
   email: true
 }
 // whatever
}
// obviously, for just required, there are easier ways
var checkRules = {required:true}; 
$('#form-id input').each(function(i, elem)
{
   /check\[\d\]\[(name|type)\]/.test(elem.name)
   rules[elem.name] = checkRules
});
孤独陪着我 2024-12-06 08:07:12

更多解决方法:默认情况下,您可以在必填字段上指定名为 required 的类。


另一种选择是以编程方式填写 rules 选项(此要点):

假设一个简单的示例 HTML 具有以下正文:

<body>
    <form id="example">
    <input  name="course[0][name]" />
    <input  name="course[0][type]" />

    <input  name="course[1][name]" />
    <input  name="course[1][type]" />

    <input type="submit" name="some_name" value="Go" id="some_name">
    </form>
</body>

相应的 javascript 通过 filter 确定匹配元素:

    $(document).ready(function(){

        var options = { rules : {} }

        // put your regex in here ...
        var courses = $('input').filter(function(){
            return this.name.match(/course\[([0-9]+)\]\[name\]/);
        });

        for (var i = courses.length - 1; i >= 0; i--){
            options["rules"][courses[i].name] = { required : true };
        };

        $("#example").validate(options);
    });

More a workaround: By default, you can specify a class named required on the fields that are required.


Another option would be to fill up the rules options programmatically (complete code in this gist):

Assuming a simple example HTML with the following body:

<body>
    <form id="example">
    <input  name="course[0][name]" />
    <input  name="course[0][type]" />

    <input  name="course[1][name]" />
    <input  name="course[1][type]" />

    <input type="submit" name="some_name" value="Go" id="some_name">
    </form>
</body>

The corresponding javascript to determine matching elements via filter:

    $(document).ready(function(){

        var options = { rules : {} }

        // put your regex in here ...
        var courses = $('input').filter(function(){
            return this.name.match(/course\[([0-9]+)\]\[name\]/);
        });

        for (var i = courses.length - 1; i >= 0; i--){
            options["rules"][courses[i].name] = { required : true };
        };

        $("#example").validate(options);
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文