Tsallaka zuwa abun ciki
Justice Tech School

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

Which collects all form values?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.