Sub topic 12
Making errors accessible: aria-describedby, error summary, focus
This chapter assumes you can write basic HTML forms, know what a label is, and can read short JavaScript event handlers. A red border and a message below a field work for sighted mouse users. A screen reader user needs the error to be announced, tied to its field, and reachable. This chapter covers how to do that.
Link each error to its field
Two attributes connect an error message to its input.
aria-invalid="true"tells assistive technology the current value is not valid. Screen readers usually announce "invalid entry" when the field gets focus.aria-describedbypoints to theidof the message. The screen reader reads that text after the label.
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email"
required aria-invalid="true" aria-describedby="email-error">
<p id="email-error">Error: Enter an email address in the format [email protected]</p>When the user focuses the field, a screen reader reads something like "Email address, edit, invalid entry, Error: Enter an email address in the format [email protected]". The word "Error:" at the start of the text makes the message clear even where aria-invalid is not announced. The message also names the problem and the fix in text, so it does not depend on color.
If the field already has hint text, list both ids, separated by a space: aria-describedby="email-hint email-error".
Add an error summary
Field-level errors are easy to miss after a failed submit, especially on a long form. An error summary is a block at the top of the page that lists every problem, with each item linking to its field.
<div id="error-summary" role="alert" tabindex="-1">
<h2>There is a problem</h2>
<ul>
<li><a href="#email">Enter your email address</a></li>
<li><a href="#postcode">Enter a postcode, like SW1A 1AA</a></li>
</ul>
</div>Write each link's text to match the inline message. Each link's href is the id of its input, so activating it moves the user to the field.
Move focus deliberately
After a failed submit, the page must tell keyboard and screen reader users something happened. There are two common patterns.
| Pattern | What gets focus | Best when |
|---|---|---|
| Focus the summary | The summary container (`tabindex="-1"`) | The form has several fields or several errors |
| Focus the first invalid field | The first field that failed | The form is short, with one or two fields |
The tabindex="-1" attribute makes the summary focusable by script without adding it to the Tab order. Focusing it makes a screen reader read the heading and the list.
form.addEventListener('submit', (event) => {
if (!form.checkValidity()) {
event.preventDefault();
const summary = document.getElementById('error-summary');
summary.hidden = false;
summary.focus();
}
});This assumes <form novalidate>, so the browser's own bubbles do not replace your messages.
Common mistakes
- Using
role="alert"on every inline message. Several alerts can fire at once and interrupt each other. Use one summary instead. - Leaving
aria-invalid="true"on a field after the user fixes it. Remove it when the value becomes valid. - Pointing
aria-describedbyat a hidden element and expecting it to be read. Hidden withhiddenordisplay: noneis skipped in most cases, so keep the message visible while it applies. - Moving focus to the summary and then moving it again. Pick one target.
- Writing a summary link text such as "Error 1". Use the actual message.
Exercise
A user submits a signup form with three invalid fields. Where should focus go, what should each summary item contain, and which two attributes connect an inline message to its input?
Show answer
Move focus to the error summary, which has tabindex="-1". Each summary item is a link whose text matches the inline message and whose href is the id of the invalid input. Connect the inline message to its input with aria-invalid="true" on the input and aria-describedby pointing to the message's id.