Created
February 23, 2022 11:26
-
-
Save dazecoop/a08bb0fb2bc594a86da4919e51569ca7 to your computer and use it in GitHub Desktop.
Vanilla JS form submit listener, preventDefault with native browser required fields valid check
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
document.addEventListener('submit', function (e) { | |
const form = e.target; | |
let isValid = form.checkValidity(); | |
if (!isValid) { | |
return false; | |
} | |
e.preventDefault(); | |
let data = new FormData(form); | |
// Do stuff | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment