Skip to content

Instantly share code, notes, and snippets.

@dazecoop
Created February 23, 2022 11:26
Show Gist options
  • Save dazecoop/a08bb0fb2bc594a86da4919e51569ca7 to your computer and use it in GitHub Desktop.
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
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