Trying to validate a form that I don't know the contents of and display a summary at the top of the page using jQuery Validate. I don't want messages next to each field. I can get it working fine onsubmit, and if I disable onfocusout/onblur/onkeyup, everything works nicely. At the moment I've been trying to do this through showErrors, but I'm open minded.
I'm under the impression that jQuery Validate only brings back the single element that failed when it fires onblur or keyup. What happens right now is that my summary gets overwritten each time, and when it fires onblur, the summary can come back empty (presumably since errorMap only includes the one element that failed). What I'd like is to update or regenerate my summary so that it stays correct as users move through the form.
I kind of think I might need to save a reference to my validator and then loop through validator.errors() or .elements(), but I'm not sure where to go with that approach. My entire validateform() function, which is called from any submit button being clicked, is below.
function validateform() {
$("form").validate({
showErrors: function(errorMap, errorList) {
var errorCount = this.numberOfInvalids();
if (errorCount > 0) {
if (errorCount == 1) {
$("#errorContainer h3").html("Your form contains " + errorCount + " error, see details below.");
}
else { /*two or more errors */
$("#errorContainer h3").html("Your form contains " + errorCount + " errors, see details below.");
}
var summary = '';
$.each(errorMap, function(key, value) {
var field = $('input[name="' + key + '"],select[name="' + key + '"],textarea[name="' + key + '"]');
field.addClass('err');
var fieldname = field.attr("rel");
summary += '<p><span class="errorkey">' + fieldname + '</span> - <span class="errorvalue">' + value + "</span></p>";
});
$('#errorList').html(summary);
$('#errorContainer').slideDown();
}
},
onfocusout: false,
onkeyup: false,
onclick: false
});
}