MVC 表单验证
我的表单验证遇到问题。我有一个带有 Required
属性的表单类,并且在 web.config 中将 ClientValidationEnabled 设置为 true。我的页面上也有此调用 @{Html.EnableClientValidation();}
我正在使用 ajax 表单,并在提交之前选项来捕获验证。这是我所拥有的:
$(document).ready(function () {
var options = {
beforeSubmit: ensureValid
};
$('#applyForm').ajaxForm(options);
});
function ensureValid(formData, jqForm, options) {
var result = $('#applyForm').validate();
console.log(result.valid());
return result.valid();
}
代码命中了 EnsureValid 函数,但即使我知道应该触发某个属性,它也会继续执行控制器中的操作。
谢谢你的见解,
布伦娜
I am having trouble with my form validation. I have a form class with the Required
attribute on it and I have ClientValidationEnabled to true in my web.config. I also have this call on my page @{Html.EnableClientValidation();}
I am using ajax form with the before submit option to catch the validation. Here is what I have:
$(document).ready(function () {
var options = {
beforeSubmit: ensureValid
};
$('#applyForm').ajaxForm(options);
});
function ensureValid(formData, jqForm, options) {
var result = $('#applyForm').validate();
console.log(result.valid());
return result.valid();
}
The code hits the ensureValid function but keeps continuing to the action in the controller even when I know a property should fire.
Thank you for any insight,
Brenna
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用的是 asp.net-mvc-3,我建议您考虑使用 jquery.validate 来执行验证。它的设置要容易得多,并生成更清晰的代码。您可以在我的博客文章(我还介绍了您可能遇到的一个可能的问题)。
If you are using asp.net-mvc-3, I would recommend at looking at using jquery.validate to perform your validation. It's far easier to setup, and generates cleaner code. You can see how to set this up in my blog post (I also cover a possible problem you could run into).