Loading video player…

Style Invalid Fields Beyond Color

In this lesson, you made the Donate a Tool form show a wrong field only once someone has used it, and in words as well as color:

  • :invalid matches too early. An empty required field fails its checks from the moment the page loads, so a rule on :invalid turns the form red before anyone has typed.
  • :user-invalid waits for the user. It matches a field that fails its checks once its value has changed and focus has left it, or once someone tries to send the form. Tabbing past a field without changing it doesn’t count. The red goes away as soon as the value is valid. :user-invalid is Baseline Widely available.
  • Color alone isn’t enough. With no color, as the Rendering panel’s Achromatopsia emulation shows, the red border looks almost like the gray ones, and the more common red-green types turn it a dull brown. 1.4.1 Use of Color asks that color is never the only way a page shows what something means.
  • Add an icon and words. Each field gets an error message right after its input, with an icon marked aria-hidden="true". The message is hidden until the input right before it matches :user-invalid.
  • Leave aria-describedby pointing at the hint alone. A description includes hidden elements too, so the field would describe an error before anything is wrong.
  • What a screen reader gets. The Accessibility pane shows the field’s invalid state as true. The browser sets it itself from the field’s own checks, with no aria-invalid in the HTML, unlike the server’s error in the last lesson.
  • Where the message reaches a screen reader. The message sits right after the field, where reading on finds it. Announcing it as it appears needs JavaScript, which is outside this course. When the server finds the error instead, the page comes back with its errors already in place and connected.

Here’s the email field’s input and its new message as the lesson ends (the label and hint above them are unchanged from the last lesson):

Language: HTML
<input type="email" id="email" name="email"
  autocomplete="email" required
  aria-describedby="email-hint">
<p class="error">
  <span class="error-icon" aria-hidden="true">!</span>
  Enter an email address with an @, such as [email protected].
</p>

And the rules that style it:

Language: CSS
input:user-invalid {
  border-color: var(--color-error);
}

.error {
  display: none;
}

input:user-invalid + .error {
  display: block;
}

00:00 In the last lesson, you connected the donate form’s instructions and the server’s error to their fields. This time, Tomasz Nowak opens the same form, empty, to offer the library his pressure washer.

00:13 Kwame wants the browser to flag a wrong field before anything is sent. So he gives every input that matches the :invalid pseudo-class a red border.

00:23 Tomasz hasn’t typed a thing, and three fields are already red.

00:29 An empty required field fails its checks from the moment the page loads. So the form tells Tomasz he’s wrong before he’s had a chance to start.

00:39 Change one word in the selector. The :user-invalid pseudo-class matches the same fields, but only after the user has interacted with them. Save, and the empty form loads with every border gray. :user-invalid is Baseline Widely available.

00:56 Now watch Tomasz fill it in. He types his name and presses Tab. In the email field, he types his address but forgets the at sign. While he’s still typing, the border stays gray.

01:08 He presses Tab to move on, and the email field turns red.

01:13 That’s the timing you want. A field counts as used once its value has changed and focus has left it. Next, Tomasz tabs straight past the Tool field to the Condition menu.

01:25 The Tool field is still empty, and it’s required. Your turn: pause the video and predict whether the Tool field turns red. It stays gray. Tomasz passed through it, but he never changed its value, so it doesn’t count as used.

01:42 Then he presses Offer this tool. Trying to send the form counts as using every field. The browser stops the form, and the empty Tool field turns red too. Focus goes back to the email field, with the browser’s own message under it.

01:58 He adds the missing part of his address, and the red goes away as soon as it’s valid.

02:04 But when Tomasz left the email field, the only thing that changed was a color. Open DevTools, then the Rendering panel, and emulate a vision deficiency. Achromatopsia, the extreme case, shows the page with no color at all.

02:21 Now the red border looks almost the same as the gray borders around it. The more common red-green types turn it a dull brown. Watch out: a color that stands out to you can look like the default to someone with color blindness, or in print.

02:37 Success criterion 1.4.1, Use of Color, asks that color is never the only way a page shows what something means. So give each field an error message in words, with an icon in front of it.

02:51 The icon gets aria-hidden, because the words already say what it means. Hide the messages to begin with. Then a second rule uses the plus combinator: when an input matches :user-invalid, the message right after it shows.

03:07 Run Tomasz’s walk again, still with no color. When he leaves the email field, the icon and the message appear under it.

03:17 Leave aria-describedby pointing at the hint alone. A description includes hidden elements too, so the field would describe an error before anything’s wrong.

03:27 Now check what a screen reader gets. The Accessibility pane shows the field as invalid. The browser sets that itself. The server’s error needed aria-invalid.

03:38 The message sits right after the field, where reading on finds it. Announcing it as it appears needs JavaScript.

03:46 Style errors with :user-invalid, so they appear once someone has used the field. Then show them with an icon and words, not color alone. The donate form now waits for Tomasz, then says what’s wrong in words.

04:02 When the server finds the mistake instead of the browser, the page comes back with its errors already in place and connected. The next lesson, Summarize Errors After Submitting, adds a list of them at the top of the form.

Become a Member to join the conversation.