Jquery 验证器与二维数组
我将从我的问题的示例开始:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
作为替代方案,你可以做这样的事情
As an alternative you can do something like this
所以,我做了一些搜索,看起来规则对象中的键是简单的 jQuery 选择器。根据这个问题,有一个可以使用的正则表达式过滤器。在两者之间,上面的正则表达式应该可以工作。
最初的想法:
......你不需要预测它们,你的服务器已经知道它们。只需将该数字输出到脚本标记中,然后循环即可。哎呀,您甚至可以为您提供服务器循环。
如果这不是一个选项,那么您可以使用 jQuery:
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:
更多解决方法:默认情况下,您可以在必填字段上指定名为
required
的类。另一种选择是以编程方式填写
rules
选项(此要点):假设一个简单的示例 HTML 具有以下正文:
相应的 javascript 通过 filter 确定匹配元素:
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:
The corresponding javascript to determine matching elements via filter: