【问题标题】:Jquery Validation general errors?Jquery Validation 一般错误?
【发布时间】:2011-12-29 19:51:42
【问题描述】:
我希望能够检查表单的有效性。如果有任何错误,我想显示一个常规错误 div,其中包含查看表单的说明。谁能帮我看看语法?
【问题讨论】:
标签:
jquery
validation
jquery-plugins
【解决方案1】:
$(function(){
$('form').submit(function(event){ //when form is submitted
var errors = false; //errors start as false
if($('.email').val() === ''){ //this is where you can validate fields
errors = true; //if validation fails errors = true
}
if(errors){ //if it does not pass validation
event.preventDefault(); //don't submit form
$('errorDiv').show(); //show the hidden div that tells the user there are errors
}
});
});
【解决方案2】:
这似乎是一个很好的起点:
$(document).ready(function() {
var submitted = false;
('.selector').validate({
showErrors: function(errorMap, errorList) {
if (submitted) {
var summary = "You have the following errors: \n";
$.each(errorList, function() { summary += " * " + this.message + "\n"; });
alert(summary);
submitted = false;
}
this.defaultShowErrors();
},
invalidHandler: function(form, validator) {
submitted = true;
}
});
});
大卫·富特顿的法庭:Display both summary and individual error messages using the jQuery validation plugin