Jquery Validate Plugin: A confirm alert on submit
Hello,
I am using the validate plugin to perform validations on a form.
I would like to show a confirm message ( alert with ok and cancel ) upon submit and successful vaildation.
I have tried the following but it does not work:
$("#myForm").validate({
rules: {
----
---
},
showErrors: function(errorMap, errorList) {
var errors = this.numberOfInvalids();
if (errors){
var message = errors == 1
? 'You missed 1 field. It has been highlighted below.'
: 'You missed ' + errors + ' fields. They have been highlighted below.';
}
if( $("#error").is(':hidden') ){
$("#error").show();
}
$("#error").html(message);
}else {
$("#error").hide();
answer = confirm("Are you sure you want to submit?");
//alert(answer);
if (answer == true){
return true;
}
else{
//alert("In FALSE");
return false;
}
}
this.defaultShowErrors();
}
});
The form submits
even If I click "Cancel". Is there anything wrong with my code above?
Appreciate any suggestions/help.
Thanks.