DEV Community

FrozonFreak
FrozonFreak

Posted on Edited on Originally published at thewebhig.hashnode.dev

Don't validate complex fields on every keystroke (Web HIG tip #19)

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:

  • The first characters of a valid value look wrong, so the field starts shouting before the person has finished
  • Keyboard and screen-reader users can get a stream of error updates while they are still editing
  • A live hint, such as a character count or a password-requirement list, is not the same thing as marking the field invalid

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:

<form action="https://example.com/account" method="post" id="account">
  <label for="email">Email</label>
  <input id="email" name="email" type="email" autocomplete="email" required
         aria-describedby="email-error">
  <p id="email-error" hidden></p>
  <button type="submit">Save</button>
</form>
<script>
  const input = document.getElementById('email');
  const error = document.getElementById('email-error');
  input.addEventListener('input', () => {
    if (!input.validity.valid) {
      error.hidden = false;
      error.textContent = 'Enter a valid email.';
    } else {
      error.hidden = true;
    }
  });
</script>
Enter fullscreen mode Exit fullscreen mode

Wait until the person leaves the field, or until they submit, before showing that first invalid state. The form has novalidate so the browser's native submit message does not replace the custom one; the script still reads validity. This is a client-side illustration, not a complete account form, and it is not a substitute for server-side checks:

<form action="https://example.com/account" method="post" id="account" novalidate>
  <label for="email">Email</label>
  <input id="email" name="email" type="email" autocomplete="email" required
         aria-describedby="email-error">
  <p id="email-error" hidden></p>
  <button type="submit">Save</button>
</form>
<script>
  const form = document.getElementById('account');
  const input = document.getElementById('email');
  const error = document.getElementById('email-error');

  function showEmailError(show) {
    if (show) {
      error.hidden = false;
      error.textContent = 'Enter a valid email.';
      input.setAttribute('aria-invalid', 'true');
    } else {
      error.hidden = true;
      error.textContent = '';
      input.removeAttribute('aria-invalid');
    }
  }

  function emailFailed() {
    return input.validity.valueMissing || input.validity.typeMismatch;
  }

  input.addEventListener('blur', () => {
    if (input.value !== '' && emailFailed()) showEmailError(true);
  });

  form.addEventListener('submit', (event) => {
    if (emailFailed()) {
      event.preventDefault();
      showEmailError(true);
      input.focus();
    }
  });

  input.addEventListener('input', () => {
    if (!error.hidden && !emailFailed()) showEmailError(false);
  });
</script>
Enter fullscreen mode Exit fullscreen mode

The script uses the browser's built-in type="email" and required checks. 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 is required; a partial value such as name, 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 :invalid can 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-invalid for 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 or change for 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 input handler 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

  • For complex fields, run the first "this is invalid" check on blur or on submit, not on every input event
  • Keep help visible from the start when it helps people meet a requirement (for example password rules). That is guidance, not an error
  • Once a field has already been marked invalid, you may update or clear the message as the person edits
  • Don't style :invalid as 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-invalid for that style, with a fallback for older browsers, and don't assume the exact moment it matches is identical everywhere. Styling :required is fine when it only marks that the field is required, not that it is already wrong
  • Preserve what the person typed. A failed check should not wipe the value
  • Repeat the important checks on the server. This tip is about when the interface first talks, not about skipping validation

Quick 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.

Top comments (0)