Errors are where forms most often fail their users. A message appears somewhere on the page, and the person who needs it either cannot see it, cannot find it, or cannot tell which field it refers to.
Colour is not a message
A red border indicates that something is wrong to those who can perceive it. It does not say what is wrong, and it says nothing at all to a screen reader user or to someone who cannot distinguish the colour.
Every error needs text explaining the problem, and that text needs to be programmatically associated with the field it concerns. Colour and icons are reinforcement, never the message itself.
Associating the message with the field
The aria-describedby attribute points at the id of the element containing the message, so it is announced when the field receives focus.
<label for="email">Email address</label> <input type="email" id="email" name="email" aria-describedby="email-error" aria-invalid="true"> <p id="email-error" class="field-error"> Enter an email address in the format name@example.com </p> The aria-invalid attribute marks the field as failing validation, which is announced as part of the field's state. Both should be removed or set to false once the problem is corrected, or the field will continue to report an error the user has already fixed.
The describedby attribute accepts several ids separated by spaces, which lets a field carry both a permanent hint and an error message.
Write messages that say what to do
A message reading invalid input tells the user nothing they did not know. A useful message identifies the field, states what is wrong, and says what an acceptable value looks like.
Avoid blaming the user. Phrasing that describes what is needed reads better than phrasing that describes what they did wrong, and it is more useful because it points at the solution.
Be specific about format requirements. If a password needs a certain length and character mix, say so in the message, and ideally show the rules before the user starts rather than after they fail.
The error summary
On a form with several problems, a summary at the top listing every error is considerably more useful than making the user hunt through the page.
Each entry should be a link to the field it describes, so activating it moves focus straight there. Put the summary immediately after the form heading, give it a heading of its own, and move focus to it after a failed submission so the user is taken to the explanation rather than left wherever they were.
This pattern is well established in government service design for good reason: it works on long forms where a per-field message alone leaves people scrolling.
Announcing errors that appear dynamically
Where errors appear without a page reload, a screen reader will not notice new content unless it is in a live region.
Marking the summary container with a polite live region means new content inside it is announced once the user pauses. An assertive region interrupts immediately, which is appropriate for a critical failure and unnecessarily disruptive for a routine validation message.
The container must exist in the page from the start. Inserting a new element that already carries the attribute frequently fails to announce, because the software was not watching that element.
When to validate
Validating on every keystroke reports an error before the user has finished typing, which is irritating and inaccurate. Validating only on submission means a long form is completed before any problem surfaces.
The usual compromise validates a field when it loses focus, then revalidates on input only once it has already failed, so a user correcting a mistake sees the message clear as soon as the value becomes acceptable. The CSS user-invalid pseudo-class follows similar timing and is worth using for the visual side.
Errors need more than the field
Where a failed submission reloads the page, the title should reflect that something went wrong, since a screen reader announces it on load and it is the first indication available. Including the number of problems is a small addition that tells the user how much work is ahead.
Do not lose the user's work
A form returning with errors must retain everything already entered. Clearing fields on a failed submission is the fastest way to make someone abandon a form, and it happens most often with password fields and file inputs, the latter because a file cannot be repopulated for security reasons. Where a file was uploaded, say so and confirm it is still attached rather than silently presenting an empty control.