← Back to HTML & CSS, page 6

html-css-0110 · Medium mid

Why is :invalid a poor way to show form errors, and what does :user-invalid do better?

:invalid matches as soon as a value breaks a rule, even before the user has typed. :user-invalid matches only after interaction, so error styles appear at a better time. It is newer, so provide a fallback, and always add error text linked with aria-describedby.

Full answer

:invalid matches any control whose value breaks its rules, and this is true from the moment the page loads. An empty required field matches :invalid before the user has typed anything. If you style :invalid with a red border, the whole form is red on the first view. That is unfriendly.

:user-invalid matches only after the user has interacted with the control. In practice that means after the field lost focus or after the user tried to submit. Then the error style shows at a time when it helps. :user-valid is the matching state for correct values.

Older ways to get the same result:

  • A class added by a script on the blur event.
  • The :placeholder-shown trick, which only works for empty fields and needs a placeholder.
  • :focus:invalid to show the error only while the user is typing.

:user-invalid is newer. Check the support table. A fallback is a script that adds a class after blur. Because an unsupported selector makes a whole rule invalid, write the :user-invalid rule on its own.

Colour alone is not enough for an error. Add text, and connect it to the field with aria-describedby. The message should say what to do, such as "Enter a valid email address".

On load the field matches :invalid but not :user-invalid, so it is plain. After the user types abc and leaves the field, it gets the red border and the message appears.

The check types real key presses, because a script that sets the value does not count as user interaction.

html
<style>
  input:user-invalid { border: 2px solid #c00; }
  input:user-invalid + .msg { display: block; }
  .msg { display: none; color: #c00; }
</style>
<label for="e">Email</label>
<input id="e" type="email" required aria-describedby="m">
<span class="msg" id="m">Enter a valid email address.</span>

The check typed real key presses and moved focus away, because a script that sets the value does not count as user interaction. 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

  • :invalid matches from page load.
  • :user-invalid waits until the user has interacted.
  • Fallback: add a class on blur with a script.
  • Add text and aria-describedby, not colour only.
  • Keep the :user-invalid rule separate for support reasons.

Common mistakes

  • Styling :invalid directly, so the form shows errors before the user has typed.
  • Showing the error only with a red colour and no text.
  • Putting :user-invalid in the same selector list as older selectors, which can invalidate the whole rule in browsers that do not know it.

Follow-ups

  • How would you show errors in a browser that lacks :user-invalid?
  • Why does a script that sets value not trigger :user-invalid?

Tips

  • Say when each one starts to match: on load or after interaction.
  • Mention the fallback script, and the need for text and aria-describedby.