I am using the jQuery Validation plugin to validate the form on the client side. In addition to the colorful style with invalid form fields, my client requires a pop-up message to be displayed. I want to show this message only when the submit button is pressed, because otherwise the user will go crazy. I tried the following code, but errorList is always empty. Anyone knows the right way to do something like this.
function popupFormErrors(formId) {
var validator = $(formId).validate();
var message = '';
for (var i = 0; i < validator.errorList.length - 1; i++) {
message += validator.errorList[i].message + '\n';
}
if (message.length > 0) {
alert(message);
}
}
$('#btn-form-submit').click(function(){
$('#form-register').submit();
popupFormErrors('#btn-form-submit');
return false;
});
$('#form-register').validate({
errorPlacement: function(error, element) {},
highlight: function(element) { $(element).addClass('invalid-input'); },
unhighlight: function(element) { $(element).removeClass('invalid-input'); },
...
});
Update
From the information in the accepted answer, I came up with this.
var submitClicked = false;
$('#btn-form-submit').click(function() {
submitClicked = true;
$('#form-register').submit();
return false;
});
$('#form-register').validate({
errorPlacement: function(error, element) {},
highlight: function(element) { $(element).addClass('invalid-input'); },
unhighlight: function(element) { $(element).removeClass('invalid-input'); },
showErrors: function(errorsObj) {
this.defaultShowErrors();
if (submitClicked) {
submitClicked = false;
... create popup from errorsObj...
}
}
...
});
source
share