Accessible HTML Form Errors and Feedback
Explain the rule before entry
Provide a label and visible format instructions. aria-describedby can reference the instruction by ID, adding an accessible description while the label supplies the accessible name. Do not rely on placeholder text alone.
<label for="ref">Reference</label><p id="help">Use two letters followed by two digits.</p><input id="ref" pattern="[A-Z]{2}[0-9]{2}" aria-describedby="help">The input has the name Reference and a separate description.
Associate an error after a failed attempt
After validation identifies an error, show a specific correction and use aria-invalid="true" to communicate the invalid state. Refer to the error text using aria-describedby; multiple space-separated IDs can preserve the original instruction. This is a static example of a failed state, not an automatic validation system.
<label for="email">Email</label><p id="hint">Use your contact address.</p><input id="email" type="email" value="alex" aria-invalid="true" aria-describedby="hint error"><p id="error">Enter an address such as alex@example.com.</p>The two descriptions resolve to visible text; the native syntax also fails.
Update feedback after correction
ARIA states do not perform native validation. When using custom feedback, update the message and aria-invalid after checking the value. The small JavaScript preview uses a check button and an existing polite live region to report the result. The browser and assistive technology decide announcement timing.
<label for="code">Code</label><input id="code" required pattern="[0-9]{3}" aria-describedby="feedback"><button type="button" id="check">Check code</button><p id="feedback" aria-live="polite"></p><script> const field=document.getElementById("code"); document.getElementById("check").addEventListener("click", () => { const valid=field.checkValidity(); field.setAttribute("aria-invalid", String(!valid)); document.getElementById("feedback").textContent=valid?"Code accepted locally":"Enter three digits."; });</script>Check an empty field, then type 123 and check again. The error becomes a local success message.
Common mistakes and practice
Do not mark untouched fields invalid merely because they are required. Do not use color alone, and do not repeatedly interrupt users while typing. For larger forms, provide a clear error summary with links to fields and preserve input. Test focus and announcements with assistive technology.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Accessible HTML Form Errors and Feedback — Knowledge Check
Apply the control concepts taught in this lesson.
What provides an accessible name for the field in the examples?
When should aria-invalid=true be set?
Does aria-invalid=true make checkValidity() return false by itself?
How can a description reference both help and error text?
What is a useful correction strategy for larger forms?