Sub topic 5
Writing and Presenting Error Messages
Assume you know basic HTML form elements, JavaScript or TypeScript, and how a browser submits form data. This chapter focuses on turning validation failures into messages that help users recover quickly.
What an effective error message does
An error message should answer three questions:
- What is wrong?
- Where is it wrong?
- What should the user do next?
“Invalid input” answers none of these questions. “Enter an email address in the format [email protected]” identifies the problem and gives a correction.
Use the same language as the field label. If the label says “Email address,” write “Enter an email address,” not “The email field is invalid.” Avoid blaming the user. Describe the required action.
Error messages should be specific but not expose sensitive information. For example, a sign-in form should not reveal whether an email address exists in the system.
Associate messages with fields
Every field needs a visible label. Hints can explain requirements before the user makes an error. An error message should be connected to its input with aria-describedby, which tells assistive technology to read the related text. Use aria-invalid="true" when the current value fails validation.
<div>
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email" required
aria-describedby="email-hint email-error" aria-invalid="true">
<p id="email-hint">We only use this to send your receipt.</p>
<p id="email-error">Enter an email address in the format [email protected].</p>
</div>Keep the error text close to the field. Do not rely on color alone. A red border can reinforce the message, but the text must carry the meaning for people who cannot distinguish colors.
Choose when to show errors
Validation timing affects how helpful the form feels.
| Timing | Useful for | Common risk |
|---|---|---|
| On input | Formatting feedback, such as password requirements | Interrupts users while they are typing |
| On blur | Checking a field after the user leaves it | Can feel aggressive on every field |
| On step change | Preventing progress with incomplete data | Delays feedback until navigation |
| On submit | Final protection for all invalid data | Gives the user many errors at once |
A practical strategy is to validate on submit or step change, then validate a field on blur after the user has interacted with it. Once an error is visible, update it on input when the correction becomes clear.
Do not remove a message merely because the user changed a value. Recheck the value against the actual rule. Client-side validation improves feedback, but the server must validate again before accepting data.
Present errors in a recoverable way
Show an inline message beside each invalid field. For a long form, also show a summary at the top that links to each problem. After submit, move keyboard focus to the summary or the first invalid field so the user knows what happened.
A summary might say:
- “Fix the 2 errors below.”
- “Email address: enter a valid email address.”
- “Full name: enter your full name.”
Preserve the user’s valid entries. Never clear the entire form because one field failed. If a server error occurs, explain whether the user can retry and keep all entered data available.
Error flow
Exercise
Exercise
A required phone field is empty after the user selects Next. What message and presentation should you use?
Show answer
Keep the entered values, place an inline message near the field such as “Enter your phone number,” connect it with aria-describedby, mark the field aria-invalid="true", and move focus to the first invalid field or an error summary.