ASP.NET MVC 和部分客户端验证
我有一个巨大的表单,我想将其拆分为不同的步骤,但我需要在进入下一步之前验证当前步骤中的字段。
例如:我的表单有 40 个字段,它们分为 4 个步骤(每个步骤 10 个字段)。在 HTML 中,40 个页面位于同一页面中,但使用 jQuery,我只显示前 10 个页面和一个下一个按钮,第四个页面包含“提交”按钮。在进入下一步之前,我需要一种方法来调用这十个字段的验证。
有什么办法可以做到这一点吗?
问候。
I have huge form that I'd like to split in different steps, but I need to validate the fields in the current step before go to the next step.
E.g.: My form has 40 fields, and they are divided in 4 steps (10 fields each). In the HTML the 40 are in the same page, but with jQuery I only show the first 10 and a next button, the fourth page contains the "Submit" button. I need a way to invoke the validation of those ten fields before go to the next step.
Is there any way to accomplish this?
Regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设您使用默认的 mvc3 非侵入性验证,您可以使用循环和 Single 对所需元素调用验证元素验证
Assuming you use default mvc3 unobtrusive validation, you could call validation on desired elements with loop and Single Element Validation
如果您使用按钮在每个步骤之间移动,则可以修改页面,为每个步骤提供单独的表单。然后,您可以将验证限制为提交按钮的父级表单,并使用“步骤”的验证来确定是否启用/显示下一步的表单。这是一些示例代码:
If you are using buttons to move between each step, you can revise your page to have a separate form for each step. You then may limit validation to the form which is the parent of the submit button, and use the validation for the "step" to determine whether to enable/show the form for the next step. Here is some example code:
您可以使用 jQuery 验证插件。它有一个选项来在验证时忽略元素。例如,您可以使用类标记所有未显示(并且不需要验证)的字段。例如,要使用
class="ignore"
忽略所有表单元素,您可以这样做:You could use the jQuery Validation plug-in. This has an option to ignore elements when validating. So you could, for instance, mark all the fields that are not shown (and don't require validating yet) with a class. For instance, to ignore all form elements with the
class="ignore"
you'd do this:尽管通过使用属性
class="ignore"
标记元素来忽略元素是可行的,但 MVC 3 默认使用 jQuery 的验证插件,并且该插件不会验证禁用的字段
。除了可见的 10 个字段之外,您隐藏元素的代码也可以禁用这些元素,并且它们不会被验证(请注意,这些禁用的字段也不会发布到服务器)。例如
Though ignoring elements by marking them with attribute
class="ignore"
works, MVC 3 uses jQuery's validation plug-in by default and thatplug-in will not validate disabled fields
. The code where you are hiding your elements besides visible 10 fields, you can just disable those elements as well and they won't be validated (and note that those disabled fields won't be posted to the server either).e.g.