Sub topic 10
Writing error messages that help
This chapter assumes you know basic HTML form elements, can read short JavaScript snippets, and have read the earlier chapters on labels, hint text, and validation timing. Here you will learn how to write the text of an error message. Later chapters cover how errors look and how assistive technology announces them.
What an error message has to do
A user who sees an error has stopped what they were doing. The message has to answer three questions:
- Where is the problem?
- What is wrong?
- How do I fix it?
A message such as "Invalid input" answers none of these. "Enter an email address in the format [email protected]" answers all three. The field is identified by the message's position (and, in a summary, by its text), the problem is the missing or malformed address, and the fix is the example format.
The message is part of the interface, so test it like any other piece of copy. Read it aloud without the form in view. If it still makes sense, it is probably specific enough.
Name the problem and the fix
Compare these messages for the same field.
| Weak message | Why it fails | Better message |
|---|---|---|
| Invalid input | Does not say which rule failed | Enter your date of birth as day, month, year, for example 27 11 1990 |
| Error | Says nothing | Enter your email address |
| Email is not valid | Names the problem, gives no fix | Enter an email address in the format [email protected] |
| Field required | Machine wording, no subject | Enter your first name |
| Password does not meet requirements | Hides the requirements | Password must be at least 12 characters |
The pattern in the better messages is an imperative sentence that tells the user what to do ("Enter...", "Select...", "Choose..."). When the rule itself is the thing the user needs, state the rule ("Password must be at least 12 characters").
Use a different message for each failure
One field can fail in several ways, and each way needs its own message. An empty email field and a malformed email field need different advice. The Constraint Validation API makes this straightforward because the validity object exposes a separate flag for each failure.
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]';
}
if (input.validity.tooLong) {
return 'Email address must be ' + input.maxLength + ' characters or fewer';
}
return '';
}The browser's built-in messages ("Please fill out this field", "Please include an '@' in the email address") are inconsistent between browsers and languages you do not control. They also cannot be styled. For a designed form, turn off native bubbles with novalidate on the form and supply your own text, as the chapter on validation timing showed.
Say what you need, not what the user did wrong
Messages that blame the user ("You entered an invalid postcode") make people feel they failed. Messages that describe what the form needs keep the focus on the next action.
- Prefer "Enter a postcode, like SW1A 1AA" to "You entered an invalid postcode".
- Prefer "Select a delivery date" to "You forgot to choose a delivery date".
- Avoid "Oops!", jokes, and exclamation marks. Users who are already stuck do not need cheerfulness, and the tone reads badly on a payment or medical form.
Do not make the message a system report either. "Error 422: unprocessable entity" and "Validation failed on field dob" are for logs, not for people.
Be specific about the rule
Vague rules lead to repeated failures. If you only reveal a password rule after it fails, the user will submit again and fail again.
The best fix is to show the rule before the error, as hint text. Then the error message repeats a rule the user has already seen. This connects to the earlier chapter on hint text.
<label for="password">Password</label>
<p id="password-hint">At least 12 characters. Spaces are allowed.</p>
<input id="password" name="password" type="password"
autocomplete="new-password" aria-describedby="password-hint">When the field fails, say which rule failed, with the actual numbers:
- Weak: "Password too short".
- Better: "Password must be at least 12 characters. You entered 8."
Including the current count is optional, but it helps when the user cannot see what they typed.
Do not blame the format when the format is flexible
Many errors are the form's fault, not the user's. A phone number field that rejects "020 7946 0000" because of the spaces, or a name field that rejects an apostrophe, produces an error the user cannot fix sensibly. Before writing a message, ask whether the rule should exist at all.
- Accept spaces, hyphens, and brackets in phone numbers and strip them in code.
- Accept postcodes in lower case and normalize them.
- Do not reject names for containing apostrophes, hyphens, spaces, or non-Latin letters.
An error you can avoid by accepting more input is better than the best-written error message.
Handle the empty field and the wrong field differently
Required-field errors are the most common, and they should be the shortest. Use the label as the subject so the message is unambiguous when read alone, for example in a summary list.
- "Enter your first name"
- "Select a country"
- "Choose a contact method" (for a radio group, where the question is in the
legend)
Choose the verb to match the control. "Enter" fits text fields, "Select" fits dropdowns and checkboxes, and "Choose" is common for radio buttons. Using the wrong verb is a small thing, but it makes the form feel careless.
Make each message make sense alone
Messages are read in at least three places: next to the field, in an error summary at the top of the page, and by a screen reader. In the summary, the message appears as a link without the field beside it. Two fields that both say "Enter a number" cannot be told apart there.
Put the subject in the message:
- "Enter the number of adults"
- "Enter the number of children"
not two copies of "Enter a number".
Reflect the same message in code
Keep the message text in one place and reuse it for the inline error and the summary. That way the two cannot drift apart. The example below builds both from one data structure.
const messages = {
email: {
valueMissing: 'Enter your email address',
typeMismatch: 'Enter an email address in the format [email protected]',
},
postcode: {
valueMissing: 'Enter your postcode',
patternMismatch: 'Enter a postcode in the format SW1A 1AA',
},
};
function messageFor(input) {
const rules = messages[input.name];
if (!rules) return '';
for (const flag of Object.keys(rules)) {
if (input.validity[flag]) return rules[flag];
}
return '';
}
function collectErrors(form) {
return [...form.elements]
.filter((el) => el.name && messageFor(el))
.map((el) => ({ id: el.id, text: messageFor(el) }));
}collectErrors returns a list of { id, text } pairs. The inline error uses text directly, and the summary renders each pair as a link to #id. The summary and focus handling are covered in the accessibility chapter.
Server-side messages
Client-side checks can be bypassed and cannot know everything, such as whether an email address is already registered. The server must validate again, and its messages should follow the same rules.
- Return field-level errors keyed by field name, not one combined string.
- Return the same wording a client-side check would use.
- Do not leak sensitive detail. On a sign-in form, "Email or password is incorrect" is safer than saying which one was wrong, because it does not confirm that an account exists.
// Example JSON body for a failed submission (HTTP 422)
const response = {
errors: {
email: 'Enter an email address in the format [email protected]',
postcode: 'Enter a postcode in the format SW1A 1AA',
},
};Common mistakes
- Using only "Required" or an asterisk as the message after a failed submit.
- Writing one generic message for every failure of a field.
- Wording the message as a code or an exception name.
- Blaming the user, or using humour.
- Clearing the message before the user has had a chance to read it, or leaving it after the field is fixed.
- Rejecting valid input such as spaces in phone numbers.
- Using the same text for two different fields in a summary.
A diagram of the decision
The flow below shows how to choose a message when a field fails.
Exercise
A postcode field fails with the message "Invalid". Rewrite the message for two cases: the field is empty, and the field contains "12345" while the form expects a UK postcode. Then say which of the two messages should include an example, and why the message should also work inside an error summary.
Show answer
Empty: "Enter your postcode". Wrong format: "Enter a postcode in the format SW1A 1AA". The wrong-format message needs the example because the user has typed something and needs to see the expected shape. The empty message only needs to say what to enter. In an error summary the message appears as a link without the field beside it, so each message must name its subject ("postcode") to be understood alone.
Exercise
A sign-in form shows "No account exists for this email address" when the email is unknown and "Wrong password" when the password is wrong. Give one reason this is a problem and write a replacement.
Show answer
The two messages let an attacker discover which email addresses have accounts. Replace both with one message: "Email address or password is incorrect".