Forms and Validation in JavaScript
Listen to the submit event, call preventDefault and read values with FormData or element.value. Use the Constraint Validation API to check inputs.
Example
const f = document.createElement("form");
f.innerHTML = '<input name="email" type="email" required> <button>Send</button>';
document.body.appendChild(f);
f.addEventListener("submit", (e) => {
e.preventDefault();
console.log(Object.fromEntries(new FormData(f)), f.checkValidity());
});Exercise
Stop the page reload.
e.()
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.