Skip to content

Instantly share code, notes, and snippets.

@boydaihungst
Created March 31, 2020 07:30
Show Gist options
  • Save boydaihungst/4358f22c290373e37ee6b5f32df752a6 to your computer and use it in GitHub Desktop.
Save boydaihungst/4358f22c290373e37ee6b5f32df752a6 to your computer and use it in GitHub Desktop.
fix jquery validate need to click submit twice to submit form
var form = $('form');
if (form.find('[data-val-remote]').length > 0) {
form.find('[type="submit"]').on("click enter", function (evt) {
var interval = setInterval(function () {
// keep check if there are any remote pending then prevent submit button from click
if (Object.keys($('form').validate().pending).length) {
evt.preventDefault();
} else {
// if all remote were response check form valid or not
if (!form.valid()) {
clearInterval(interval);
}
// if form valid then submit and stop interval
form.submit();
clearInterval(interval);
}
// loop 100ms
}, 100);
return false;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment