--- url: "https://www.w3.org/WAI/WCAG22/Techniques/aria/ARIA21" titulek: "ARIA21: Using aria-invalid to Indicate An Error Field | WAI | W3C" h1: "Technique ARIA21:Using aria-invalid to Indicate An Error Field" popis: "" jazyk: "en" kanonicka: "" drobecky: [] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 06:29:15" znaku: 5586 slov: 732 kodovani: "utf-8" sha256: "acdd2c4720de6027d9a8ae3d5f5b6ba7ee3bf00df0b178b1c53fd29294df6e05" --- # Technique ARIA21:Using `aria-invalid` to Indicate An Error Field ## About this Technique This technique relates to [3.3.1 Error Identification](https://www.w3.org/WAI/WCAG22/Understanding/error-identification) (**Sufficient**). This technique applies to HTML with [Accessible Rich Internet Applications](https://www.w3.org/TR/wai-aria/). Techniques are examples of ways to meet Web Content Accessibility Guidelines (WCAG). They are **not required** to meet WCAG. Content can satisfy the normative requirements of WCAG even if it does not use any of the documented techniques. See [About WCAG Techniques](https://www.w3.org/WAI/WCAG22/Techniques/about). ## Description This technique demonstrates how `aria-invalid` may be used to identify fields that have failed validation. The `aria-invalid` attribute can be used as part of a custom validation pattern instead of using the HTML `required` attribute coupled with specific input types (for example: `type="email"`). The `aria-invalid` attribute should not be set to `"true"` before input validation is performed. Setting `aria-invalid` to `"false"` is the same as not placing the attribute for the form control. ## Examples ### Example 1: Using `aria-invalid` on required fields The `aria-invalid` attribute is used on required fields that are empty when the form is submitted. A message above the form tells the user that there are form fields that need to be completed. ##### HTML: ```

``` ##### CSS: ``` label.failed { background:#CE0000; color:#fff; } ``` ##### JavaScript: These are the two JavaScript functions that set the `aria-invalid="true"` attribute and then create the validation error message: ``` const requiredFields = ['first', 'last', 'email']; let errorCount = 0; requiredFields.forEach(fieldId => { const field = document.getElementById(fieldId); if (field.value.trim() === '') { field.setAttribute('aria-invalid', 'true'); document.querySelector(`label[for='${fieldId}']`).classList.add('failed'); errorCount++; } }); if (errorCount > 0) { errText.textContent = `Please complete all ${errorCount} required ${errorCount === 1 ? 'field' : 'fields'} and retry`; } ``` [Working example: Using `aria-invalid` on required fields](../../working-examples/aria-invalid-required-fields/). ### Example 2 #### Identifying errors in data format In this example, `aria-invalid` and `aria-describedby` are used together to indicate an error when the personal identification number (PIN), email address, or start date are not in the expected format. The error message is associated with the field using `aria-describedby`, and `aria-invalid` makes it easier to programmatically find fields with errors. #### Features - Adds an `aria-invalid="true"` attribute on a form field if the entered data is missing or not in the expected format: - The PIN contains fewer than 4 characters; - The email address doesn't meet the required format; - The last name must contain at least two letters; - The policy start date is before today's date. - Displays an error message which is programmatically connected to the relevant form field using the `aria-describedby` attribute. ##### HTML: ```
Error: PIN is required
``` ##### CSS: ``` .error { border: #CE0000 thin solid; } .errtext { background: #EEEEFF url('error-icon.svg') no-repeat right 0.25em center; color:#CE0000; display:block; margin-bottom: 1em; max-width: fit-content; padding: .25em 1.6em .25em .25em; } ``` ##### JavaScript: JavaScript is needed to add `aria-invalid`, `aria-describedby`, and `class` attributes as well as appending the error text. This function is the relevant part of the larger validation script: ``` function addFieldError(field, message) { const errId = field.id + '-errormsg'; const errDiv = document.createElement('div'); errDiv.className = 'errtext'; errDiv.id = errId; errDiv.textContent = message; field.parentNode.appendChild(errDiv); field.setAttribute('aria-describedby', errId); field.setAttribute('aria-invalid', 'true'); field.classList.add('error'); } ``` [Working example: Identifying errors in data format](../../working-examples/aria-invalid-data-format/). ## Related Resources *No endorsement implied.* - [Supported States and Properties: WAI-ARIA](https://www.w3.org/TR/wai-aria/#states_and_properties) - [Using aria-invalid For Error Indication](https://www.deque.com/blog/aria-invalid-error-indication/) ## Tests ### Procedure For each form control that relies on `aria-invalid` to convey a validation failure: 1. Check that `aria-invalid` is not set to true when a validation failure does not exist. 2. Check that `aria-invalid` is set to true when a validation failure does exist. 3. Check that the programmatically associated labels / programmatically associated instructional text for the field provide enough information to understand the error. ### Expected Results - Checks #1-3 are true. Back to Top