Created
March 31, 2020 07:30
-
-
Save boydaihungst/4358f22c290373e37ee6b5f32df752a6 to your computer and use it in GitHub Desktop.
fix jquery validate need to click submit twice to submit form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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