When to use this pattern
Every form needs this pattern. Validate on submit and show all errors at once: a summary at the top with links to the fields, and the exact message at each field. Messages that appear while someone is still typing often disturb screen reader users; if you validate early, do it when the field loses focus.
More on labels, required fields and autocomplete is in our guide to accessible forms.
Roles, states and properties
- Every field has a visible
<label>; format hints are shown before submitting and linked witharia-describedby. - On error:
aria-invalid="true"on the field and the error message added toaria-describedby. - The message describes the error and the fix in text, not by colour or an icon alone.
- After submitting, an error summary (a heading with
tabindex="-1") receives focus and lists each error as a link to its field. - Input is kept; nobody has to fill in the form again.
Keyboard interaction
| Key | Effect |
|---|---|
| Tab | Moves through the fields to the submit button. |
| Enter | Submits the form; with errors, focus moves to the error summary. |
| Enter | On a link in the error summary: jumps straight into the field with the error. |
Code
<label for="mail">Email address</label>
<p id="mail-hint">For example [email protected]</p>
<input id="mail" type="email" autocomplete="email"
aria-describedby="mail-hint mail-err"
aria-invalid="true">
<p id="mail-err" class="err">
Error: enter an email address with an @,
for example [email protected].
</p>
<!-- top of the form, receives focus after submit -->
<div class="err-summary">
<h2 tabindex="-1">There are 2 errors</h2>
<ul><li><a href="#mail">Email address is missing an @</a></li></ul>
</div>Common mistakes
- Errors marked with a red border only (fails 1.4.1 Use of Color and 3.3.1 Error Identification).
- “Invalid input” with no hint about what is expected (3.3.3).
- The message appears somewhere on the page but is not linked to the field.
- The submit button stays disabled until everything is right, so nobody learns what is missing.
- The form is empty after submitting.
Related WCAG criteria
Sources
Are your components built like this?
The free check finds many issues automatically. In the audit a person tests every component with a keyboard and a screen reader.