jQuery 验证回调函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6224670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery Validation Callback Function
提问by Vincent Catalano
I currently have a form within a lightbox frame on my webpage. When a user submits the form with invalid data, a div appears with a list of form errors at the top of the form. The problem is: I need some sort of jQuery validation callback to resize the lightbox after the errors div appears. As far as I'm aware, there is no way to do this with the jQuery lightbox.
我目前在我的网页上的灯箱框架内有一个表单。当用户提交包含无效数据的表单时,表单顶部会出现一个带有表单错误列表的 div。问题是:在出现错误 div 后,我需要某种 jQuery 验证回调来调整灯箱的大小。据我所知,jQuery 灯箱无法做到这一点。
回答by mcgrailm
you to supply the invalid callback function which you can find documented here, like so
$(".selector").validate({
invalidHandler: function(form, validator) {
var errors = validator.numberOfInvalids();
if (errors) {
//resize code goes here
}
}
})
回答by Sebastian G
Alternatively, you can use the errorPlacement callback function in order to act on the specific element that failed validation. As an example, the code below uses the errorPlacement callback to set the class of each invalid form element's parent div tag to "error" and then removes the "error" class once the element passes validation :
或者,您可以使用 errorPlacement 回调函数来处理验证失败的特定元素。例如,下面的代码使用 errorPlacement 回调将每个无效表单元素的父 div 标签的类设置为“error”,然后在元素通过验证后删除“error”类:
form.validate({
rules: {
Name: {
required: true
},
Email: {
required: true
, regex: "^[0-9a-zA-Z.+_\-]+@{1}[0-9a-zA-Z.+_\-]+\.+[a-zA-Z]{2,4}$"
}
},
messages: {
Name: {
required: "Please give us your name"
},
Email: {
regex: "Please enter a valid email address"
}
},
errorPlacement: function(error, element) {
element.parent().addClass("error");
},
success: function(element) {
$("#" + element.attr("for")).parent().removeClass("error");
}
});