如何在提交前执行服务器端验证
表单包含输入元素。使用模糊的服务器回调验证输入的值 事件安德勒。
如果输入了错误的值并按下了提交按钮,则表单将以错误的值提交: 验证方法未完成,但表单提交操作方法已执行。
如何强制 Validate() 方法在执行表单提交方法之前完成?
使用 jquery、jqueryUI、ASP .NET MVC2。
<form id="Form" class='form-fields' method='post' action='/Report/Render'>
<input id='test' name='test' value='test' />
<input id='_submit' type='submit' value='Show report' />
</form>
$('#test').bind({
blur: function (e) {
if (!Validate(e.target)) {
cancel(e);
}
}
});
更新
按照BobTodd评论中的要求,这里是验证方法:
function Validate(elem) {
var i,
res;
$.ajax('Grid/Validate' );
{
data: $("#Form").serializeArray(),
//async: false,
type: 'POST',
error: function (jqXHR, textStatus, errorThrown) {
elem.skipBlur = true;
errorMessage(decodeErrorMessage(jqXHR.responseText === undefined ? '' : jqXHR.responseText,
textStatus, errorThrown));
elem.focus();
elem.select();
elem.skipBlur = false;
elem.ischanged = true;
res = false;
},
success: function (data) {
elem.ischanged = false;
for (i = 0; i < data.length; i += 1) {
$('#' + data[i].name).val(data[i].value);
}
res = true;
}
});
return false; // res;
}
Update2
所有答案都建议在未执行验证的情况下阻止提交方法运行。这提供了非常糟糕的用户体验:验证需要一些时间。用户应在提交按钮中按两次才能提交表单。如何允许单击提交表单?使用 Validate() 同步方法可以解决此问题,但我不确定是否使用同步调用来解决此问题。
Form contains input element. Entered value is validated using server callback from blur
event andler.
If wrong value is entered and submit button is pressed, form is submitted with wrong value:
Validate method is not completed but form submit action method is executed.
How to force Validate() method to be completed before form submit method is executed ?
jquery, jqueryUI, ASP .NET MVC2 are used.
<form id="Form" class='form-fields' method='post' action='/Report/Render'>
<input id='test' name='test' value='test' />
<input id='_submit' type='submit' value='Show report' />
</form>
$('#test').bind({
blur: function (e) {
if (!Validate(e.target)) {
cancel(e);
}
}
});
Update
As required in BobTodd comment here is validate method:
function Validate(elem) {
var i,
res;
$.ajax('Grid/Validate' );
{
data: $("#Form").serializeArray(),
//async: false,
type: 'POST',
error: function (jqXHR, textStatus, errorThrown) {
elem.skipBlur = true;
errorMessage(decodeErrorMessage(jqXHR.responseText === undefined ? '' : jqXHR.responseText,
textStatus, errorThrown));
elem.focus();
elem.select();
elem.skipBlur = false;
elem.ischanged = true;
res = false;
},
success: function (data) {
elem.ischanged = false;
for (i = 0; i < data.length; i += 1) {
$('#' + data[i].name).val(data[i].value);
}
res = true;
}
});
return false; // res;
}
Update2
All answers suggest to prevent submit method to run if validation is not preformed. This provides very bad user experience: validate takes some time. User shoudl press two times in submit button for form to submit. How to allow form to submit using single click ? Maksing Validate() sync method alllws this but i'm not sure is using sync call reasonable solution for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须在表单提交时重新运行 Validate 方法并在那里取消它。
看一下 jQuery Validate 插件,它支持删除验证方法,使用 mvc 可以非常轻松地工作。
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
http://docs.jquery.com/Plugins/Validation/Methods/remote#options
You will have to rerun the Validate method on form submit and cancel it there.
Look at the jQuery Validate plugin, it supports remove validation methods that work super easy w/ mvc.
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
http://docs.jquery.com/Plugins/Validation/Methods/remote#options
只需禁用提交按钮,直到验证完成。
Just disable the submit button until validation is complete.
难道你最好将 jquery 绑定到表单的提交事件,否则它只是在用户离开文本框时...
Wouldnt you be best served binding that jquery to the submit event of the form instead otherwise its just when the user leaves the textbox...
验证字段时,如果元素有效,则将
valid
类添加到该元素。然后,在提交表单时,只需检查所有字段是否都具有该类:When you validate a field, add a
valid
class to the element if it's valid. Then, when submitting the form, simply check if all of the fields have that class :