Sub topic 16
Accessible error messages
This chapter assumes you know how labels name controls, how built-in validation attributes work, and how the Constraint Validation API (validity, setCustomValidity) reports problems. It shows how to make validation errors reach people who use screen readers or only a keyboard.
Why the default bubble is not enough
Browsers show a validation bubble when a form fails to submit. The bubble disappears when focus moves, its wording and styling vary by browser, and screen reader support is uneven. You also cannot reread it. Production forms usually show errors in the page itself.
An accessible error message has four properties:
- It is text in the page, not only a red border or an icon.
- It says what is wrong and how to fix it.
- It is programmatically tied to its control.
- It is announced or reachable when it appears.
Connect the message to the control
Put the message in an element with an id. Reference it from the control with aria-describedby. Set aria-invalid="true" while the value is wrong. A screen reader then reads the label, the state, and the message when the control gets focus.
<form id="signup" novalidate>
<div id="error-summary" tabindex="-1" hidden></div>
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email"
required aria-describedby="email-error">
<p id="email-error" class="error"></p>
<button type="submit">Subscribe</button>
</form>The novalidate attribute turns off the browser bubble. It does not turn off validation. The Constraint Validation API still works, and checkValidity() still returns the correct result.
Show the errors with JavaScript
The script reads validity on each control, picks a message, and moves focus to the first invalid control. Moving focus makes the screen reader read the label, state, and message together.
const form = document.querySelector('#signup');
const summary = document.querySelector('#error-summary');
function messageFor(input) {
if (input.validity.valueMissing) return 'Enter your email address.';
if (input.validity.typeMismatch) return 'Enter an address like [email protected].';
return input.validationMessage;
}
function validateField(input) {
const error = document.getElementById(input.id + '-error');
if (input.checkValidity()) {
input.removeAttribute('aria-invalid');
error.textContent = '';
return true;
}
input.setAttribute('aria-invalid', 'true');
error.textContent = messageFor(input);
return false;
}
form.addEventListener('submit', (event) => {
const invalid = [...form.elements].filter(
(el) => el.willValidate && !validateField(el)
);
if (invalid.length > 0) {
event.preventDefault();
summary.hidden = false;
summary.textContent = `The form has ${invalid.length} error(s). Fix them and submit again.`;
invalid[0].focus();
} else {
summary.hidden = true;
}
});
form.addEventListener('input', (event) => {
if (event.target.getAttribute('aria-invalid') === 'true') {
validateField(event.target);
}
});The input listener clears an error as soon as the user fixes the value. It does not nag before the first failed submit.
Common mistakes
- Color only. A red border alone fails users who cannot see color. Always include text.
- Placeholder as message. Placeholder text disappears on input and is not a reliable description.
- Message not linked. A paragraph below the field is invisible to a screen reader user until they find it by browsing. Use
aria-describedby. - Stale
aria-invalid. Remove it when the value becomes valid. - Vague text. "Invalid input" gives no way to recover. Name the field and the fix.
Exercise
Why does the example call invalid[0].focus() after a failed submit, and which attribute makes the screen reader read the message when focus lands?
Show answer
Focusing the first invalid control brings keyboard and screen reader users to the problem. The aria-describedby attribute links the control to its message element, so the message is read after the label and the aria-invalid state.