HTML Form Error Summary Accessible

Show form validation errors in an accessible summary with aria-live and focus management.

HtmlForm Validation

Code

htmlsnippet
18 lines
<form id="form" novalidate>
  <div role="alert" aria-live="assertive" id="errors" hidden></div>
  <label for="email">Email</label>
  <input id="email" name="email" type="email" required aria-describedby="email-error">
  <span id="email-error" class="error" hidden></span>
  <button type="submit">Submit</button>
</form>
<script>
  form.addEventListener('submit', (e) => {
    if (!form.checkValidity()) {
      e.preventDefault();
      const errors = [...form.elements].filter(el => !el.validity.valid);
      document.getElementById('errors').innerHTML = errors.map(el => '<li>' + el.name + ': invalid</li>').join('');
      document.getElementById('errors').hidden = false;
      errors[0].focus();
    }
  });
</script>

How the snippet works

2 ARIA attributes are set explicitly, covering states native semantics alone do not expose.

The form collects input via type="email", type="submit" fields, each tied to a <label> so every control announces what it is.

Script and styles anchor onto ids form, errors, email, email-error and classes error, so behavior and styling stay decoupled from the tag names.

What to watch for

  • ARIA attributes only help when they match real behavior — an announced state the script never updates is worse than none.

Statements that matter

<form id="form" novalidate>
<label for="email">Email</label>
<input id="email" name="email" type="email" required aria-describedby="email-error">

Interactive Tools for This Topic

FAQ

Which non-generic elements does the markup use?
`<form>`, `<label>`, `<input>`, `<button>`, `<li>` — each carries native semantics (or a browser behavior) that generic containers do not.