Repository navigation
Don't validate complex fields on every keystroke (Web HIG tip #19) #37
frozonfreak
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Someone starts typing name@example.com into the email field on example.com/account. At the first letter the field turns red and an error appears: "Enter a valid email." They have not finished the address. Each extra keystroke flickers the same message. The check is running on every input event.
Web HIG tip #19 (Quick #53): Do not validate on every keystroke for complex fields — validate on blur or submit.
Why it matters
Email, phone, password, date and similar fields are incomplete for most of the time someone is typing. Checking them on every keystroke often means:
Quick #53 is Quick-only guidance (no rule ID of its own). The same timing appears in §2.11, Forms Contract, written for commerce checkout, application settings, and auth flows: field-level checks inline on blur, form-level checks on submit, and no validation before the user has interacted, unless the value was pre-filled.
Before and after
This field reports an error on the first keystroke, while the address is still being typed:
Wait until the person leaves the field, or until they submit, before showing that first invalid state. The form has
novalidateso the browser's native submit message does not replace the custom one; the script still readsvalidity. This is a client-side illustration, not a complete account form, and it is not a substitute for server-side checks:The script uses the browser's built-in
type="email"andrequiredchecks. Those checks are not a complete definition of a valid address, and they are not identical in every browser. An empty email is valid unless the field isrequired; a partial value such asname, typed before@example.com, already fails the syntax check. Native constraint validation also has its own timing: a message may appear on submit, and CSS:invalidcan match immediately — including an empty required field on page load, and that partial email while someone is still typing. If you style:invalid, the field can look wrong before blur or submit. Prefer:user-invalidfor the invalid style. It is Baseline widely available (since May 2026; newly available in late 2023 with Chrome 119, Firefox 88, and Safari 16.5). It generally waits until the user has committed a change (blur orchangefor a text or email field) or has tried to submit, not until the first keystroke. Once it matches, it typically stays matched while the value is still invalid, including as the person continues typing, until the value is valid or the form is reset. Exact timing can still differ by browser and control type, so do not treat it as a guaranteed match for Quick #53, and keep a fallback for older browsers.The last
inputhandler only clears an error that was already shown. That is not the first error on every keystroke. The blur handler also waits for a non-empty value, so an empty required field is shown on submit rather than when someone tabs through. A password-requirement list or a character count can also update as someone types, without marking the field invalid. This snippet does not implement an error summary, a pre-filled invalid value, or a framework form library. If a value arrives already invalid, §2.11 allows a check before further interaction.Do this instead
inputevent:invalidas the invalid state: it matches immediately, including an empty required field on page load and a partial email while someone is still typing. Use:user-invalidfor that style, with a fallback for older browsers, and don't assume the exact moment it matches is identical everywhere. Styling:requiredis fine when it only marks that the field is required, not that it is already wrongQuick check for your app
On a test copy of a form such as example.com/account, type a partial email, tab away, then submit with an empty or incomplete value. The first invalid message should appear on blur or submit, not on the first key. Confirm the error clears at a sensible time, and that a live hint does not behave like a stream of errors. Try it with the keyboard and with a screen reader your audience uses. Suitable automated checks can flag a missing error or a missing field association; they cannot tell you the timing feels right. Pair them with a manual pass on the browsers you support. A green suite is evidence for what it actually exercised, not proof that every field waits long enough.
The Web HIG is an open set of guidelines for how the web should behave, not a component library. Design systems define how it looks. The Web HIG defines how it behaves.
All reactions