Web development

Connect validation messages to form fields

When a field has a validation error, describe the problem in text and connect that message to the field. Aria-invalid can expose its state, while aria-describedby associates the explanation.

Worked example

HTML example · save as example.html and open in a browser

<label for="email">Email address</label>
<input id="email" type="email" aria-invalid="true" aria-describedby="email-error">
<p id="email-error">Enter an address with a name and domain, such as [email protected].</p>

How it works

The error identifies the affected field and gives a useful correction. It does not rely only on a red border or icon. In an interactive form, set these attributes after validation and clear the error when the value becomes acceptable.

Limits and mistakes

Aria attributes do not validate input or enforce submission rules. Avoid showing every possible error before the user has interacted. If errors appear dynamically, plan focus and announcements so they are discoverable without becoming repetitive.

Check the result

Inspect the field’s description and invalid state in the accessibility tree. Trigger and correct an error in the implemented form, confirming the message and state both update.

Sources

Reference documentation for the behavior used in this guide: