Sub topic 11
Accessible labels and error messages
This chapter assumes you can build a controlled form with useState, handle onSubmit, and write a validation function that returns an errors object. Here you will make that form usable with a screen reader and a keyboard.
Why labels matter
A screen reader announces an input by reading its accessible name. Without one, a user hears only "edit text". A placeholder is not a reliable label. It disappears as the user types, and many assistive tools do not treat it as a name.
A <label> also enlarges the click target. Clicking the label text focuses the input, or toggles it for checkboxes and radios.
Connecting a label to an input
There are two ways to link a label to its input.
- Wrap the input inside the label, as in the earlier
NameFieldexample. - Give the input an
idand set the label'shtmlForto the same value.
In JSX the attribute is htmlFor, because for is a reserved word in JavaScript. Each id must be unique on the page. If a component renders more than once, generate ids with the useId hook instead of hard-coding them.
import { useId } from 'react';
export default function TextField({ label, ...props }) {
const id = useId();
return (
<>
<label htmlFor={id}>{label}</label>
<input id={id} {...props} />
</>
);
}Connecting an error to its field
Printing an error message under a field is not enough. A screen reader user who tabs into the field will not hear the message unless the field points to it. Two attributes do this:
aria-invalidtells assistive tools that the current value is not valid.aria-describedbyholds theidof the element that describes the field. The screen reader reads that text after the label.
The LoginForm from the validation chapter already uses both:
<input
id="email"
name="email"
value={values.email}
onChange={handleChange}
aria-invalid={Boolean(errors.email)}
aria-describedby={errors.email ? 'email-error' : undefined}
/>
{errors.email && <p id="email-error">{errors.email}</p>}When errors.email is empty, aria-describedby is undefined, so React omits the attribute. This avoids pointing at an element that does not exist.
Announcing errors that appear after submit
Errors that appear after the user presses submit are new content. A screen reader will not read them unless you tell it to. Two common options:
- Give the error element
role="alert". It is announced as soon as it is inserted. - Move focus to the first invalid field after a failed submit, so its label and error are read at once.
<p id="email-error" role="alert">{errors.email}</p>Use role="alert" for errors that arrive on submit. For errors shown while the user types, it can interrupt them on every keystroke. Show those errors only after the field is touched, as covered in the previous chapter.
Common mistakes
- Using only color, such as a red border, to show an error. Always include text.
- Using
placeholderas the only label. - Writing vague messages such as "Invalid". Say what is wrong and how to fix it, as in "Use at least 8 characters."
- Disabling the browser's validation with
noValidateand then not providing your own accessible messages. - Duplicated
idvalues, which break the label and error links without any warning.
Exercise
A password field has the id password and the error paragraph has the id password-error. Which two attributes on the input make a screen reader announce the error when the user focuses the field with an invalid value, and what value does each take?
Show answer
aria-invalid={true} marks the value as invalid, and aria-describedby="password-error" links the input to the error paragraph so its text is read after the label. When there is no error, set aria-describedby to undefined so the attribute is not rendered.
Exercise
Why should a form component that renders several times on one page use useId instead of a fixed string for the input's id?
Show answer
Ids must be unique in the document. A fixed string would be duplicated in every instance, so each htmlFor or aria-describedby could point to the wrong element. useId returns a different stable id for each instance.