HTML Forms, Validation, and Accessibility That Doesn't Break
Build HTML forms that work with keyboards and screen readers, validate input natively before writing custom JS, and handle submissions without losing user data.
You'll learn how <label> and <input> connect for accessibility, how native HTML validation (required, type="email", minlength) saves you lines of JavaScript, and how FormData reads every field cleanly on submit. Assumes basic HTML and DOM event listeners.
1. Forms are where most websites quietly break
A shocking number of forms on the internet are built out of <div>s, placeholder text instead of labels, and <span onclick="..."> pretending to be a submit button. They break the moment someone hits Tab, presses Enter, uses a password manager, or relies on a screen reader.
Good HTML forms give you keyboard navigation, Enter-to-submit, autofill, and basic validation for free—as long as you use the right tags.
2. Labels are not optional (placeholders are not labels)
Placeholder text vanishes the exact second a user starts typing. If someone gets distracted halfway through a form, they are staring at a box full of text with no clue what it was asking for.
Always pair every <input> with a real <label> using matching for and id attributes:
<form id="contact-form">
<label for="email">Email address</label>
<input id="email" name="email" type="email" required />
<label for="message">Message</label>
<textarea id="message" name="message" minlength="10" required></textarea>
<button type="submit">Send</button>
</form>Two huge wins from for="email" + id="email":
- Clicking the label text focuses the input (a lifesaver for small checkboxes and mobile thumbs).
- Screen readers announce "Email address, edit text, required" instead of "edit text, blank".
Also notice name="email" and type="email": type="email" brings up the @ keyboard on phones automatically, and name is the key the browser uses when reading the form's data.
Click the word "Email address" in your browser—the cursor should jump straight inside the email input box.
3. Reading everything cleanly with FormData
Instead of calling document.querySelector separately for ten inputs, wrap the submitted <form> in new FormData(form) inside your submit listener:
const form = document.querySelector("#contact-form");
form.addEventListener("submit", (event) => {
event.preventDefault();
const data = Object.fromEntries(new FormData(form));
console.log(data);
form.reset();
});If the user left email blank or typed "banana" instead of an email address, the browser's built-in required and type="email" validation blocks the submit event automatically and focuses the invalid field—your JavaScript only runs once the browser's checks pass.
4. Custom validation messages without breaking native flow
Sometimes built-in rules aren't enough (e.g., "confirm password must match password"). You can plug custom checks right into the browser's validation system with setCustomValidity:
const password = document.querySelector("#password");
const confirm = document.querySelector("#confirm");
confirm.addEventListener("input", () => {
if (confirm.value !== password.value) {
confirm.setCustomValidity("Passwords do not match.");
} else {
confirm.setCustomValidity(""); // Empty string clears the error!
}
});Forgetting to reset setCustomValidity("") back to an empty string is the #1 bug with it—as long as it holds a non-empty message, the form considers the field permanently invalid.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
Missing name="..." on <input> | FormData skips the field completely | Add a unique name attribute to every input |
<button> inside <form> with no type | Defaults to type="submit" and submits the form! | Use type="button" for non-submit buttons |
Used placeholder instead of <label> | Screen readers and distracted humans lose context | Always include a visible <label for="..."> |
Forgot setCustomValidity("") | Field stays stuck invalid forever | Reset to "" whenever the value becomes valid |
Practice
- Why does
<button>Cancel</button>inside a<form>accidentally submit the form when clicked? - What happens if
<input id="age" type="number">has nonameattribute when you callnew FormData(form)?
Answers:
- Inside a
<form>, a<button>without atypedefaults totype="submit". Always write<button type="button">Cancel</button>. FormDataignores it completely—inputs must have anameattribute to be included.
What you learned
- Pair every input with
<label for="...">and give every input anameattribute. - Use semantic input types (
email,number,url) plusrequiredandminlengthfor free browser validation and mobile keyboards. Object.fromEntries(new FormData(form))extracts all named fields in one line.- Always put
type="button"on non-submit buttons inside a<form>.