我正在使用JQuery-validate插件来验证表单。到目前为止,只要出现错误,我都会突出显示这些字段。我希望显示警报,指示有一些错误需要更正,但是我不确定在哪里添加警报。无论我将其添加到现有代码中的哪个位置,它都会多次显示。
$(function() {
$("form[id='changeAccountSettings']").validate({
onclick: false,
onkeyup: false,
errorPlacement: function(error, element) {
var n = element.attr("name");
if (n == "first_name")
element.attr("placeholder", "Please provide a first name");
else if (n == "last_name")
element.attr("placeholder", "Please provide a last name");
else if (n == "email")
element.attr("placeholder", "Valid email address");
else if (n == "cellphone")
element.attr("placeholder", "Valid cellphone number");
},
rules: {
first_name: {
minlength: 2,
required: true
},
last_name: {
minlength: 2,
required: true
},
email: {
minlength: 6,
required: true,
email: true
},
cellphone: {
minlength: 10,
required: true,
number: true
},
},
highlight: function(element) {
$(element).addClass('has_error');
},
unhighlight: function(element) {
$(element).removeClass('has_error');
},
submitHandler: function(form) {
form.submit();
}
});
});