Sub topic 3
Writing Error Messages That Help
Assumptions: you know basic HTML, CSS, and JavaScript, and you understand forms, inputs, and buttons. This chapter focuses on helping users recover after a validation error.
Write messages that explain the fix
An error message should identify the problem and give the next action. “Invalid input” makes the user investigate. “Enter an email address in the format [email protected]” gives a clear fix.
Use the same words as the label. Place the message next to the affected field. Keep the user’s existing value unless it is unsafe to retain.
<label for="email">Email address</label>
<input id="email" name="email" type="email"
aria-invalid="true" aria-describedby="email-error"
value="sam@example">
<p id="email-error">Enter an email address in the format [email protected].</p>Show errors without relying on color
Use more than a red border. Add a visible message, an icon with text, or a clear heading such as “There is a problem with this field.” Keep sufficient contrast between text, borders, and the background.
Do not remove the message after the user starts typing unless you have confirmed that the value is valid. A consistent error state prevents users from guessing whether the problem remains.
Make errors accessible
aria-invalid="true" tells assistive technology that a value needs correction. aria-describedby connects the input to its error text, so the message can be announced with the field.
For several errors, add an error summary near the form heading. Make it a link to each invalid field. After a failed submission, move focus to the summary or the first invalid field. The summary is useful when users need an overview; field focus is faster when there is one error.
Preserve input and help recovery
Keep valid values when the form is re-rendered. Return users to the form, keep their position when practical, and identify the first correction clearly. Never clear the entire form because one field failed.
Test the complete interaction
Use the keyboard only. Confirm that focus order is logical, error links work, and focus is visible. Test with a screen reader to verify that labels, invalid states, and messages are announced. Test narrow screens and zoom. Finally, ask people unfamiliar with the form to complete realistic tasks.
Exercise
What should an error message contain?
Show answer
The problem and a specific action that fixes it.