Linking hints and errors to a field
aria-describedby connects visible help or error text to a form field, and aria-invalid="true" marks the field whose value is wrong.
Step by step
- Write the help or error as visible text next to the field, in an element with its own
id. - Link the field to it with
aria-describedby="that-id"; screen readers usually read it after the field's name. - The
<label>still gives the name; the linked text is extra help and stays visible for everyone. - Separate several ids with spaces, not commas: in
a, bthe first id becomesa,, which matches nothing. - Put
aria-invalid="true"only on the field with the problem; a correct field getsfalseor no attribute. - Remove
aria-invalid="true"once the value is fixed. None of these attributes changes how the page looks.
The card and its answer
1<label for="phone">Phone</label>
2<input id="phone" name="phone"
3 type="tel" autocomplete="tel"
4 aria-describedby="phone-hint">
5<p id="phone-hint">
6 We call to confirm your guitar lesson.
7</p>
→Answer: The hint sentence, read as extra help after "Phone"
This is how it's right
<label for="email">E-mail</label>
<input id="email" name="email" type="email"
aria-invalid="true"
aria-describedby="email-hint email-error">
<p id="email-hint">We never share your address.</p>
<p id="email-error">Please add an @ to your e-mail.</p>
Exercises for this topic
Key rule
Link help and errors with aria-describedby (ids separated by spaces), and mark only the wrong field with aria-invalid.
Where you'll meet this
You meet these attributes in sign-up, login and booking forms that check the input with JavaScript and show a message under each field.