Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 38 ยท Forms and Dynamic Content

Form Errors and Validation

Report form errors so everyone can fix them: error summaries, inline messages, aria-invalid, aria-describedby, focus management, and avoiding novalidate traps.

Everyone makes mistakes in forms. Whether they can recover depends on whether your page tells them what went wrong, where, and how to fix it. WCAG has four related criteria:

  • 3.3.1 Error Identification (A): detect the error and describe it in text.
  • 3.3.3 Error Suggestion (AA): suggest a fix when you know one.
  • 3.3.4 Error Prevention (AA): for legal, financial or data-changing submissions, allow review, confirmation or reversal.
  • 4.1.3 Status Messages (AA): announce updates that appear without moving focus (see live regions).

Write good messages

A good message names the field, says what is wrong, and says how to fix it. Avoid blame and codes.

Weak Better
"Invalid input" "Enter a date in the format DD/MM/YYYY."
"Error 14" "Choose a country from the list."
"Required" "Enter your email address."
"Password rejected" "Password must have at least 12 characters. You entered 8."

Place the message next to its field (usually below the label) and in text, not only color or an icon.

Mark and connect the error

For each invalid field:

  1. Set aria-invalid="true".
  2. Add the message with an id, and reference it with aria-describedby (keep the hint id there too).
  3. Keep the label unchanged.
HTML
<label for="age">Age</label>
<input id="age" type="number" aria-invalid="true" aria-describedby="age-hint age-err">
<p id="age-hint">Whole years, 18 or older.</p>
<p id="age-err"><strong>Error:</strong> enter a number that is 18 or more.</p>

Screen readers will say "Age, edit text, invalid entry, Whole years..., Error: enter a number...".

Error summary at the top

For forms submitted to the server, or long forms, show a summary of all problems above the form and move focus to it. Each item links to its field. This tells users how many errors there are and lets them jump to each one.

HTML
<div role="alert" tabindex="-1" id="summary">
  <h2>There are 2 problems</h2>
  <ul>
    <li><a href="#email">Enter your email address</a></li>
    <li><a href="#age">Enter an age of 18 or more</a></li>
  </ul>
</div>

Also set the page <title> to start with "Error:" so screen reader users get a signal when the page reloads.

When to validate

  • On submit is the safest default. It does not scold people mid-typing.
  • On blur (when leaving a field) is fine for simple formats, as long as you do not jump focus.
  • Never validate on every keystroke with a flashing message, and do not move focus while someone is typing.

Do not disable the submit button as the way of showing "something is wrong": a disabled button is skipped by Tab and offers no explanation. Keep it enabled and explain on click.

The native validation trap

Browsers have built-in validation bubbles (required, type="email", pattern). They are better than nothing, but they vary by browser, disappear quickly and are not always announced reliably. A popular approach: add novalidate to the form, then validate yourself with the Constraint Validation API and show your own accessible messages. If you do this, you must provide the messages; novalidate without custom handling removes all feedback.

Accessible validation with summary and focus

Submit the form empty and tab through the result: the summary takes focus, each link jumps to its field, and each field carries its own message.

Error prevention for important actions

For orders, legal commitments, deleting data or financial transfers, give at least one of:

  • Reversible: let users undo or cancel after submit.
  • Checked: validate input and let users fix it before final submission.
  • Confirmed: show a review page before the final "Place order".

A confirmation step with a plain summary and an explicit button is the simplest option.

Authentication without puzzles

3.3.8 Accessible Authentication (Minimum), WCAG 2.2 AA: do not require a cognitive function test (remembering a password, solving a puzzle, transcribing characters) unless an alternative exists. In practice: allow password managers and paste in password fields, use autocomplete="current-password", and offer options like email links, passkeys or biometric sign-in. Do not block paste.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which is the best error message?

    It names the problem and tells the user how to fix it (3.3.1 and 3.3.3).
  2. 2. Which attributes tie an error message to its field for assistive technology?

    aria-invalid marks the state; aria-describedby points to the message text.
  3. 3. After a failed submit, where should focus go?

    Moving focus to a summary makes the problems discoverable and lets users jump to each field.
  4. 4. Why is a disabled submit button a poor way to indicate errors?

    Users get no feedback; keep the button enabled and explain the problem when it is activated.

Summary

  • Describe errors in text, say how to fix them, and place them next to the field.
  • Use aria-invalid and aria-describedby; add a focus-moved summary with links.
  • Validate on submit, keep the button enabled, and never leave the user without feedback if you use novalidate.
  • For consequential actions allow review, confirmation or undo; support password managers.

Next: Live Regions and Status Messages