Web Accessibility & WCAG (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
How do you implement Accessible Form Validation?
senior LevelCode ExampleFormsSystem Design
In Simple Words: When an input fails validation:
- Set
aria-invalid="true"on the input. - Link the error message element to the input using
aria-describedby:
html
<label for="user-age">Age:</label>
<input id="user-age"
type="number"
aria-invalid="true"
aria-describedby="age-error-msg" />
<span id="age-error-msg" role="alert" class="error-text">
You must be at least 18 years old.
</span>
When focus lands on the input, the screen reader announces: "Age, edit text, invalid entry. You must be at least 18 years old."