HTML Form Error Summary Accessible
Show form validation errors in an accessible summary with aria-live and focus management.
Code
<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
WCAG Color Contrast CheckerCalculate WCAG 2.1 color contrast ratios between foreground and background colors. Verify AA and AAA compliance levels for normal and large text accessibility.JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.
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.
Report a problem with this exampleOpens a pre-filled issue on GitHub.