diffsnack
HTML Accessibility Guess the result

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

  1. Write the help or error as visible text next to the field, in an element with its own id.
  2. Link the field to it with aria-describedby="that-id"; screen readers usually read it after the field's name.
  3. The <label> still gives the name; the linked text is extra help and stays visible for everyone.
  4. Separate several ids with spaces, not commas: in a, b the first id becomes a,, which matches nothing.
  5. Put aria-invalid="true" only on the field with the problem; a correct field gets false or no attribute.
  6. 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.

Play this card Next article: Live regions: aria-live and role="alert"