Full answer
Browsers can check form values without any JavaScript. You describe the rules with attributes, and the browser blocks submission and shows a message when a rule fails.
Useful attributes: required, type (email, url, number), min and max, minlength and maxlength, step and pattern (a regular expression the whole value must match). Each control has a validity object with flags such as valueMissing, typeMismatch, patternMismatch, tooShort, rangeUnderflow and customError. checkValidity() returns true or false, and reportValidity() also shows the message.
The Constraint Validation API is the scripting side. Use it when the rule cannot be written as an attribute, such as "confirm password must match password". Call setCustomValidity("message") to mark a field invalid, and setCustomValidity("") to clear it.
Typing abc in the email box sets typeMismatch, 12 in the age box sets rangeUnderflow, abc12 in the roll number box sets patternMismatch, and mismatching passwords set a customError with the message you wrote. When the values are corrected, checkValidity() returns true.
Validation in the browser is for convenience only. Anyone can bypass it, so the server must validate again. Style states with :invalid and :valid, but remember :invalid matches before the user has typed anything. A newer :user-invalid pseudo-class waits for interaction. Check the support tables before depending on it.
<form id="f">
<label for="name">Name</label>
<input id="name" name="name" required minlength="2">
<label for="email">Email</label>
<input id="email" name="email" type="email" required>
<label for="age">Age (16 to 60)</label>
<input id="age" name="age" type="number" min="16" max="60">
<label for="code">Roll number (3 capital letters, 3 digits)</label>
<input id="code" name="code" pattern="[A-Z]{3}[0-9]{3}" title="Example: ABC123">
<label for="pw">Password</label>
<input id="pw" name="pw" type="password" minlength="8" required>
<label for="pw2">Repeat password</label>
<input id="pw2" type="password" required>
<button type="submit">Register</button>
</form>
<script>
const pw = document.getElementById("pw"), pw2 = document.getElementById("pw2");
function match() { pw2.setCustomValidity(pw.value === pw2.value ? "" : "Passwords do not match"); }
pw.addEventListener("input", match);
pw2.addEventListener("input", match);
</script>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Built-in rules: required, type, min, max, minlength, maxlength, step, pattern.
- validity flags tell which rule failed; checkValidity returns true or false.
- setCustomValidity marks a field invalid and an empty string clears it.
- :invalid matches before any typing; :user-invalid waits for interaction.
- Always validate on the server as well.
Common mistakes
- trusting browser validation and skipping the server check. The browser rules can be removed in DevTools or by sending a request by hand.
- forgetting to clear a custom error with setCustomValidity(""). The field then stays invalid forever.
Follow-ups
- what does the novalidate attribute on a form do?
- What does the novalidate attribute do?
- How would you show a custom error message next to a field?
Tips
- Put a clear text hint near the field (format, limits) before the user makes an error. The title attribute is not shown on touch screens.
- Explain what you do for a confirm password field: custom validity cleared on each input.
- Say clearly that client validation is not security.