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:
:invalidmatches too early. An empty required field fails its checks from the moment the page loads, so a rule on:invalidturns the form red before anyone has typed.:user-invalidwaits 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-invalidis 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-describedbypointing 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-invalidin 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):
<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:
input:user-invalid {
border-color: var(--color-error);
}
.error {
display: none;
}
input:user-invalid + .error {
display: block;
}