Reputation: 82
How can I display one error message after validating multiple fields?
For example if i have 3 grouped text fields and i would like to show an error message ONLY after validating all three fields.
Upvotes: 0
Views: 962
Reputation: 29976
Check out the jQuery validation plugin, especially the parts around groups:
http://docs.jquery.com/Plugins/Validation/validate#toptions
Upvotes: 1
Reputation: 14435
With no code example of the form, I'm not sure what you are validating against or if each field had a unique validation, so I had to guess:
jsfiddle: http://jsfiddle.net/jensbits/vVe3r/3/
<form id="myform">
<input type="text" /><br />
<input type="text" /><br />
<input type="text" /><br />
<input type="submit" value="Submit" />
$(function() {
$("#myform").submit(function(e) {
e.preventDefault();
var validfields = true;
$("input").each(function() {
if ($(this).val() === "") {
validfields = false;
}
if (!validfields) {
$("#error").html("<span style='color:red'>Error in form</span>");
}else{
$("#error").html(""); //if submit posts back to same page
$("#myForm").submit();
}
});
});
$("input").blur(function() {
if ($(this).val() === "") {
$("#error").html("<span style='color:red'>Error in form</span>");
}else{
$("#error").html("");
}
});
});
Upvotes: 0