Error messaging and inline validation UX
Build semantic, accessible and responsive interfaces from a blank page, and explain why each layout and styling decision works.
What you will be able to do
#- Associate an error message with the field it describes.
- Design validation that does not punish users while they type.
Overview
#An error message that is only red text is invisible to a screen reader and to anyone who cannot see color. Tie each message to its field with aria-describedby, point the field at the message's id, and set aria-invalid="true" while the error is present. The message should say what is wrong and how to fix it.
<label for="email">Email address</label>
<input
id="email"
name="email"
type="email"
aria-describedby="email-error"
aria-invalid="true"
/>
<p id="email-error">Enter an email address in the form name@example.com.</p>
Timing matters. Validating on every keystroke flags a field as invalid before the user has finished typing, which is frustrating. Validate on blur for individual fields, and on submit for the whole form. When submission fails, move focus to the first invalid field or to an error summary, so keyboard and screen reader users are not left guessing why nothing happened.
<div role="alert" tabindex="-1" id="form-error">
There are 2 problems with this form.
</div>
A role="alert" region is announced as soon as its content changes, which makes it a good error summary. Use it sparingly: an alert interrupts, so reserve it for genuine failures. Success messages can use role="status", which is announced politely without interrupting.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Implement
Wire an email field to an error message so the message is announced with the field and the field is marked invalid.
starter.html<label for="email">Email</label> <input id="email" name="email" type="email" /> <p id="email-error">Enter a valid email.</p>Reveal the reference answer
reference.html<label for="email">Email</label> <input id="email" name="email" type="email" aria-describedby="email-error" aria-invalid="true" /> <p id="email-error">Enter a valid email.</p>Authored expected output: The input references the error message with aria-describedby and reports aria-invalid="true".
aria-describedby links the message to the field so it is announced on focus, and aria-invalid exposes the error state beyond color.
Exercise 2Interview
A reviewer says 'we validate every field on each keystroke so users get instant feedback'. Give a short, concrete critique and a better approach.
Reveal the reference answer
reference.textPer-keystroke validation flags fields as invalid before the user finishes, which is noisy and can be announced repeatedly by assistive technology. Validate a field on blur, validate the whole form on submit, and when submission fails move focus to the first invalid field or an error summary. Keep messages specific and linked to their fields.Authored expected output: A critique that names the premature-invalid problem and proposes blur plus submit validation with focus management.
This is a common interview prompt about balancing feedback speed against interruption and accessibility.
Common misconceptions
#- Red border and red text are enough to communicate an error.
- Validating on every keystroke gives the fastest feedback and is always best.
Follow-up practice
#- html-css-11-accessibility-tree
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.