In my test program, I use jquery.validate plugin to check user input fields as so:
<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/jquery-form-3.51.min.js"></script>
<script src="js/jquery-validation-1.15.1/jquery.validate.min.js"></script>
<form id="form-profile" method="post">
<input name="Age" type="text" />
<input name="Gender" type="text" />
<button type="submit">submit</button>
</form>
<script>
$(function() {
setValidateRules();
});
function setValidateRules() {
$("#form-profile").validate({
rules: {
Age: "required",
Gender: "required"
},
messages: {
Age: "Please fill in age",
Gender: "Please fill in gender",
},
showErrors: function(errorMap, errorList) {
alert(JSON.stringify(errorMap));
}
});
}
$("#form-profile").ajaxForm({
dataType: "json",
beforeSubmit: function(arr, $form, options) {
return $("#form-profile").valid();
},
error: function(result) {
// todo
},
success: function(result) {
// todo
}
});
</script>
Now my problem is, when I run the page via browser, after clicking submit button, the page keeps popping up alert box recursively, which makes me believe that showErrors() handler is called multiple (many many) times.
Is there a way to solve this problem ?
updated jsfiddle:
It's really not. Using
console.log()confirms that each triggering event only firesshowErrors()one time. For your demo, default triggering events areclickof submit button, andfocusoutandkeyupof each textinput.1It has to do with how/when
alert()halts all JavaScript execution until it's dismissed, and how/when certain browsers handle thealert(). (I've seen Safari get stuck in an infinite loop of alerts where aconsole.log()in its place would have fired one time.)This can be proven by simply replacing your
alert()with aconsole.log()and you can seeshowErrorsis really only fired ONE time per each triggering event.2DEMO: jsfiddle.net/1nnqf7qs/1/
1EDIT: When the
alert()is clicked, the field immediately loses focus (focusoutevent fires), which triggers anotheralert()that will appear as soon as the firstalert()dismisses. Infinite loop. When usingconsole.log(), the field never loses focus so no additional notifications are triggered.2EDIT: After initial form validation (after "lazy" turns into "eager" validation), when you click the button, you are then triggering TWO events...
focusoutandclickand theshowErrorsfunction responds accordingly by firing twice (once per each of these two different events).Do not use an
alert()for development and troubleshooting, useconsole.log()instead.See:
console.log()versusalert()Otherwise, I see no problem with how the
showErrorsfunction is being fired.You also need to put your Ajax INSIDE the
submitHandlerfunction of the.validate()method. As per docs, this is where it belongs.Then once inside the
submitHandler, you no longer need to check if the form is valid, because thesubmitHandleronly fires when the form is valid.