Recover from validation errors
Help users understand what went wrong and how to fix validation errors in forms.
When to use this pattern
Show validation errors when users:
- leave required fields empty
- enter data in the wrong format
- enter data that doesn't meet requirements
- enter conflicting information
How it works
When a user submits a form with errors:
- Show an error summary at the top of the page
- Show individual error messages next to each field
- Move focus to the error summary
- Update the page title to indicate there are errors
Error summary
Show an error summary box at the top of the form, before the first field.
<div class="alert alert-error" role="alert" aria-labelledby="error-summary-title" tabindex="-1">
<h2 id="error-summary-title">There is a problem</h2>
<ul>
<li><a href="#full-name">Enter your full name</a></li>
<li><a href="#email">Enter an email address in the correct format</a></li>
</ul>
</div>Individual field errors
Show the error message between the label and the input field. Add a red left border to the field group.
Enter your full name
<div class="form-group form-group-error">
<label class="form-label" for="full-name">Full name</label>
<span class="form-error" id="full-name-error">
Enter your full name
</span>
<input
class="form-input form-input-error"
id="full-name"
type="text"
aria-describedby="full-name-error"
>
</div>Date input with error
Writing error messages
Good error messages:
- Are specific about what's wrong
- Tell users how to fix the problem
- Use plain language
- Are concise (one sentence)
- Do not blame the user
Examples
| Instead of | Use |
|---|---|
| Invalid input | Enter your full name |
| Error: field required | Enter your email address |
| Format incorrect | Enter a date of birth in the correct format, like 27 3 1990 |
| Must be a valid email | Enter an email address in the correct format, like name@example.com |
Page title
When there are errors, update the page title to start with "Error:"
Error: Full name - Apply for a passport - GOV.KAHARAGIAAccessibility
- Use
role="alert"on the error summary - Move focus to the error summary on page load
- Use
aria-describedbyto link errors to inputs - Make error summary links go directly to the fields
- Use colour AND text/icons to indicate errors (not just colour)
Preventing errors
Before implementing error handling, try to prevent errors by:
- Using clear labels and hint text
- Showing format examples
- Using appropriate input types
- Breaking complex questions into simpler ones
- Only asking for information you need
Related components
- Alerts - for the error summary
- Text input - includes error state