Sub topic 10
9. Help users recognize, diagnose, and recover from errors
This chapter assumes you have read the earlier chapters, especially error prevention (chapter 5). You should also know basic HTML forms and what a form field, a label, and a validation message are.
What the heuristic says
Nielsen's ninth heuristic says that error messages should be expressed in plain language with no error codes. They should precisely indicate the problem and constructively suggest a solution.
Error prevention (heuristic 5) tries to stop mistakes before they happen. This heuristic covers what happens after one occurs, because prevention never catches everything. Users mistype, networks drop, servers fail, and sessions expire. A good interface treats the error moment as part of the design.
The title names three jobs:
- Recognize. The user must notice that something went wrong.
- Diagnose. The user must understand what went wrong and why.
- Recover. The user must know what to do next, and be able to do it without starting over.
A message can fail at any of these three. The sections below take them in turn.
Recognize: make the error noticeable
If users do not see an error, they cannot fix it. A common failure is a form that reloads after submit, with a small grey message at the top of a long page. The user scrolls down, sees an unchanged form, and assumes the submission worked.
Make errors noticeable in several ways at once:
- Put the message next to the field that caused it, not only at the top of the page.
- Use an icon or text prefix such as "Error:" in addition to colour. About 8% of men have some form of colour vision deficiency, and a red border alone can be invisible to them.
- Move focus to the first error, or to a summary that links to each failing field, so keyboard and screen reader users arrive at the problem.
- Keep the message on screen until the user fixes the problem. A toast that disappears after three seconds is a poor place for an error the user must act on.
The last point also touches on visibility of system status (heuristic 1). A silent failure, such as a save that fails without any message, is worse than a visible one, because the user believes the work is safe.
Diagnose: say what happened in plain language
A diagnostic message has three qualities. It uses the user's words, it names the specific problem, and it avoids blaming the user.
Compare these messages for the same failed login:
| Message | Problem |
|---|---|
| `Error 0x80070057` | Code with no meaning for the user |
| `Invalid input` | Names no field and no rule |
| `You entered the wrong data.` | Blames the user, still not specific |
| `That password doesn't match this account. Try again or reset your password.` | Specific, neutral, and offers a way forward |
Error codes are not forbidden. Support teams find them useful. The heuristic says the code should not be the message. You can show a short reference code in small text at the bottom, such as "Reference: A-4021", for the user to quote to support.
Security exceptions
Specific messages sometimes conflict with security. A login form that says "No account exists with that email" lets an attacker test which emails are registered. Many products accept a less specific message here, such as "Email or password is incorrect". This is a deliberate trade-off. If you take it, still give the user a next step, such as a reset link or a sign-up link.
Recover: suggest the fix, and make it cheap
The best message includes a fix and a quick way to apply it. Three patterns work well.
State the rule. "Password must be at least 12 characters" is better than "Password too weak", because the user knows what to type.
Offer a correction. If a user types [email protected], the interface can ask "Did you mean [email protected]?" with a button that applies the change. Offer the correction rather than applying it silently, because the guess may be wrong.
Preserve the user's work. When validation fails, keep everything the user already typed. A form that clears all fields after one error forces the user to start over, and many will leave. This is also a failure of user control and freedom (heuristic 3).
A worked code example
Here is a field in an error state. The message is written in plain language, says what is wrong, and gives an example of a valid value.
<label for="email">Email address</label>
<input id="email" type="email" value="ana.example.com" aria-invalid="true" aria-describedby="email-error">
<p id="email-error">Enter an email address that includes an @ sign, such as [email protected].</p>Three details matter here:
aria-invalid="true"tells assistive technology that the current value is not valid.aria-describedby="email-error"links the message to the input. A screen reader announces the message when the field gets focus.- The input keeps the value
ana.example.com, so the user can correct it instead of retyping it.
Compare this with the prevention example from chapter 5.
<label for="email">Email address</label>
<input id="email" name="email" type="email" required autocomplete="email">The type="email" and required attributes prevent many mistakes. They do not replace a good message. Browser default validation messages are often generic and differ between browsers. For important forms, write your own messages and check that they still appear for keyboard and screen reader users.
Showing the message from script
The next example validates a field on submit, shows the message, and moves focus to the invalid field. It runs as written in a page with a form that contains the email field above.
<form id="signup" novalidate>
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email">
<p id="email-error" hidden></p>
<button type="submit">Sign up</button>
</form>
<script>
const form = document.getElementById("signup");
const input = document.getElementById("email");
const error = document.getElementById("email-error");
form.addEventListener("submit", function (event) {
const value = input.value.trim();
let message = "";
if (value === "") {
message = "Enter your email address, such as [email protected].";
} else if (!value.includes("@")) {
message = "Enter an email address that includes an @ sign, such as [email protected].";
}
if (message) {
event.preventDefault();
error.textContent = message;
error.hidden = false;
input.setAttribute("aria-invalid", "true");
input.setAttribute("aria-describedby", "email-error");
input.focus();
} else {
error.hidden = true;
input.removeAttribute("aria-invalid");
input.removeAttribute("aria-describedby");
}
});
</script>The script handles the empty case and the missing @ case with separate messages, because they need different advice. It also clears the error state once the value is valid. Forgetting that step leaves a stale error on screen, which confuses users.
How the three jobs fit together
Edge cases and common mistakes
- Errors the user did not cause. If a server fails, do not suggest the user did something wrong. Say "We couldn't save your changes. Your work is still on this page. Try again in a moment." Include a retry button.
- Errors that arrive late. Validating only on submit makes users wait to learn about a mistake in the first field. Validating on every keystroke is also bad, because it shows "invalid email" while the user is still typing. Validating when the field loses focus is a common middle path.
- Many errors at once. Show a summary at the top that links to each field, and keep the per-field message too. Do not show only the first error, or users fix one problem and hit the next after every submit.
- Vague destructive failures. "Something went wrong" is acceptable only when you truly know nothing. Even then, say whether the action was completed, and what the user can try.
- Humour and blame. Jokes in error messages annoy users who have just lost work. Avoid "Oops!" when the consequence is serious.
- Translated messages. Error text built by joining fragments often breaks in other languages. Keep full sentences as single translatable strings.
Checking an interface against this heuristic
During a heuristic evaluation (chapter 11), ask these questions about each error state:
- Can the user tell that an error occurred, without relying on colour alone?
- Does the message say which field or step failed?
- Is it written without codes or jargon, and without blaming the user?
- Does it say how to fix the problem?
- Is the user's input still there?
- Can the user fix it in one step, or must they navigate elsewhere?
A "no" to any question is a possible violation. Chapter 12 covers how to rate how serious each one is.
Relation to other heuristics
| Heuristic | How it connects |
|---|---|
| Error prevention (5) | Prevention reduces how often errors happen. This heuristic covers those that remain. |
| Visibility of system status (1) | A clear error is a form of status feedback. |
| User control and freedom (3) | Undo and preserved input let users escape from mistakes. |
| Help and documentation (10) | When a message cannot explain enough, it should link to help. |
Exercise
A checkout page shows the red text "Invalid entry" at the top after the user submits. The page reloads with the card number, expiry date, and address fields empty. Name three violations of this heuristic and propose a fix for each.
Show answer
(1) Recognize: the message is far from the failing field and relies on colour. Fix: show a message next to the specific field, add "Error:" text or an icon, and move focus to the first error. (2) Diagnose: "Invalid entry" names no field and no rule. Fix: write "The expiry date has passed. Enter a date in MM/YY format, such as 08/29." (3) Recover: the fields were cleared. Fix: keep every value the user typed, so only the incorrect field needs to change. For card numbers, you may choose to clear that one field for security reasons, but then say so.
Exercise
A login form says "No account found for that email address." Name the heuristic trade-off, and write a message that handles it.
Show answer
The specific message helps users diagnose a mistyped email, but it also lets an attacker find out which emails have accounts. A safer message is "That email and password combination doesn't match. Check both and try again, or reset your password." It still gives the user a next step through the reset link.
Exercise
In the script example, what bug appears if the user submits an invalid email, corrects it, and submits again, and the else branch is removed?
Show answer
The error message and the aria-invalid attribute would stay on the page after the value became valid. The user would see an error next to a correct value, and a screen reader would still announce the field as invalid. The else branch hides the message and removes both attributes.