Sub topic 9
When to validate
This chapter assumes you know basic HTML form elements and can write JavaScript event handlers. It covers three moments to check a field: on submit, on blur, and while typing. It also covers how to combine them.
Why timing matters
Validation can be correct and still frustrating. An error that appears too early interrupts the user. An error that appears too late sends them back through the form to find it. The goal is to show each message when the user can act on it.
The three timings
| Timing | Event | Strength | Risk |
|---|---|---|---|
| On submit | `submit` | Never interrupts typing | Errors arrive in a batch, after the effort is spent |
| On blur | `blur` | Feedback comes when the user finishes a field | Can feel abrupt if the user only tabbed through |
| While typing | `input` | Instant feedback | Flags valid input as wrong before it is finished |
On submit
Submit validation is the baseline. Always do it, because users can skip fields, paste values, or press Enter early. It is also the only timing that catches every problem. Server-side checks run at this point too, and they are required whatever the browser does.
On blur
A field is "blurred" when focus leaves it. Checking then means the user has finished typing, so a message is not a surprise. It suits formats such as email addresses, postcodes, and dates. Do not validate a field the user has never entered. Tabbing past an empty optional field should not produce an error.
While typing
Checking every keystroke is usually wrong. After typing sam@, the email is invalid, and an error would shout at a user who is still working. Use live checks only where the feedback helps while the user types, such as a password strength meter, a character counter, or a username availability check. For those, wait a moment after the last keystroke before checking.
A practical pattern
A common approach combines the timings:
- Validate on blur the first time a user leaves a field.
- Once a field shows an error, re-check it on each
inputevent, so the message disappears as soon as the value is fixed. - Validate everything on submit, and move focus to the first invalid field.
Step 2 matters. A message that stays on screen after the user has fixed the problem makes them doubt their correction.
Implementing the pattern
The script below uses the Constraint Validation API. It assumes <form novalidate>, an input with id="email" and type="email" and required, and <p id="email-error" hidden></p> after it.
const form = document.querySelector('form');
const email = document.getElementById('email');
const error = document.getElementById('email-error');
function check() {
if (email.validity.valueMissing) {
return 'Enter your email address';
}
if (email.validity.typeMismatch) {
return 'Enter an email address in the format [email protected]';
}
return '';
}
function show(message) {
error.textContent = message;
error.hidden = !message;
email.toggleAttribute('aria-invalid', Boolean(message));
if (message) {
email.setAttribute('aria-invalid', 'true');
email.setAttribute('aria-describedby', error.id);
} else {
email.removeAttribute('aria-invalid');
email.removeAttribute('aria-describedby');
}
}
email.addEventListener('blur', () => {
show(check());
});
email.addEventListener('input', () => {
if (email.hasAttribute('aria-invalid')) {
show(check());
}
});
form.addEventListener('submit', (event) => {
show(check());
if (!form.checkValidity()) {
event.preventDefault();
form.querySelector(':invalid').focus();
}
});The input handler only runs the check when the field is already marked invalid. That gives the "fix it and the error clears" behavior without flagging a half-typed value on first entry.
Common errors
- Validating on focus or on first keystroke. The user sees an error before they have had a chance to enter anything.
- Relying only on client-side checks. Anyone can bypass them, so the server must validate again.
- Errors on untouched optional fields. Only check optional fields when they contain a value.
- Moving the layout. If a message pushes the next field down while the user is clicking it, they may miss the target. Reserve space or place messages so that they do not shift controls under the pointer.
- Announcing every keystroke. If an error region announces changes on each
inputevent, screen reader users hear constant interruptions. Update the message only when its text changes.
Exercise
A user tabs through a form without typing anything, and every required field shows an error. Which validation timing caused this, and what two changes would you make?
Show answer
Validation on blur ran for fields the user never entered. Track whether a field has been edited (or only validate on blur after the user has typed a value), and leave the "required" check for submit. Optionally, show the required error on blur only after the user has typed and cleared the field.