Sub topic 10
Writing error messages that help
This chapter assumes you can write basic HTML forms and read short JavaScript. It also assumes you know what a label is and how aria-describedby links a field to text. Here we focus on the words of an error message. Later chapters cover how errors look and how assistive technology announces them.
What a good message does
An error message has one job: help the person fix the problem and move on. A useful message does three things.
- It says which field has the problem, or makes that clear from where it sits.
- It says what is wrong, in plain words.
- It says how to fix it, ideally with an example of what is accepted.
Compare these messages for the same mistake.
| Weak message | Better message |
|---|---|
| Invalid input | Enter an email address in the format [email protected] |
| Error 422 | Enter your date of birth as day, month and year, for example 27 3 1987 |
| Field required | Enter your last name |
| Password too weak | Password must be at least 12 characters |
The weak messages blame the user or describe the system's state. The better ones tell the user what to do.
Write for the cause, not the symptom
One field can fail for different reasons. The browser's Constraint Validation API reports each reason separately through validity. A missing value and a badly formatted value need different advice, so give each its own message.
function emailMessage(input) {
if (input.validity.valueMissing) {
return 'Enter your email address';
}
if (input.validity.typeMismatch) {
return 'Enter an email address in the format [email protected]';
}
return '';
}
const email = document.createElement('input');
email.type = 'email';
email.required = true;
email.value = '';
console.log(emailMessage(email));
email.value = 'sam@';
console.log(emailMessage(email));
email.value = '[email protected]';
console.log(emailMessage(email) === '');Run this in a browser console. It prints the missing-value message, then the format message, then true once the value is valid. The function returns an empty string for a valid field, which lets the caller clear any old error.
Common mistakes
- Vague wording. "Something went wrong" gives the user nothing to act on. Name the field and the fix.
- Blame. "You entered an invalid date" sounds like an accusation. "Enter a date that is in the past" states the rule.
- Jargon and codes. Users do not know what a 422 or a regex is. Show them the format they can type.
- Rules revealed too late. If a password needs 12 characters, say so in hint text before the user types. The error then repeats a rule they already saw.
- Generic text on every field. The same "Please fill this out" on six fields forces users to work out which one is meant. Put the field's name in the message: "Enter your postcode".
- Humor and exclamation marks. Jokes annoy people who are already stuck.
Prefix and placement
The message should sit next to its field, below the label and above the input or directly below the input, and stay in the same place across the whole form. Starting the text with "Error:" is useful for screen reader users. They then hear that the text is an error even when the red styling is invisible to them. The earlier example in this tutorial used that prefix:
<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>Do not erase the explanation
Avoid replacing the whole message with a single icon or color change. The words carry the meaning. Styling can support them, but it cannot replace them.
Check your understanding
Exercise
A postcode field shows the message "Invalid". Rewrite it as a message that names the problem and the fix, and say why it is better.
Show answer
For example, "Enter a postcode in the format SW1A 1AA". It names the field, states the expected format, and gives an example, so the user can correct the value without guessing. "Invalid" only reports that something failed.
Exercise
Your validation code uses one message, "Enter a valid email", for both an empty field and the text "sam@". Which validity properties would let you split it into two messages?
Show answer
Use validity.valueMissing for the empty field ("Enter your email address") and validity.typeMismatch for a badly formatted address ("Enter an email address in the format [email protected]").