Testing aria-invalid on radio groups

When a group of radios is in error, assistive technology needs to be told that the group is invalid. The examples below test how to expose that invalid state. Every state is pre-set in the markup, so no JavaScript is needed.

Simplest approach: add aria-invalid="true" to a div with role="radiogroup". See Recommended practice.

Every example has a coloured box showing how to read it:

Poor practice: allowed, but not recommended.

Acceptable: allowed, and does part or all of the job. The box says what it does and does not give you.

Control: a baseline.

1. Controls

Baselines. The same visible error text is in every example on this page, and nothing links it to the group, so the examples test the invalid state only.

Example 1.1: fieldset

Control: The group with the error text shown and no invalid state.

Which is your favourite pet?

Error: Choose one pet.

HTML markup
<fieldset>
  <legend>Which is your favourite pet?</legend>
  <p class="error">Error: Choose one pet.</p>
  <div>
    <input type="radio" id="r1-dog" name="r1" value="dog">
    <label for="r1-dog">Dog</label>
  </div>
  <div>
    <input type="radio" id="r1-cat" name="r1" value="cat">
    <label for="r1-cat">Cat</label>
  </div>
  <div>
    <input type="radio" id="r1-rabbit" name="r1" value="rabbit">
    <label for="r1-rabbit">Rabbit</label>
  </div>
</fieldset>

Example 1.2: div with role="group"

Control: The group with the error text shown and no invalid state.

Which is your favourite pet?

Error: Choose one pet.

HTML markup
<div role="group" aria-labelledby="r2-label">
  <h4 id="r2-label">Which is your favourite pet?</h4>
  <p class="error">Error: Choose one pet.</p>
  <div>
    <input type="radio" id="r2-dog" name="r2" value="dog">
    <label for="r2-dog">Dog</label>
  </div>
  <div>
    <input type="radio" id="r2-cat" name="r2" value="cat">
    <label for="r2-cat">Cat</label>
  </div>
  <div>
    <input type="radio" id="r2-rabbit" name="r2" value="rabbit">
    <label for="r2-rabbit">Rabbit</label>
  </div>
</div>

Example 1.3: div with role="radiogroup"

Control: The group with the error text shown and no invalid state.

Which is your favourite pet?

Error: Choose one pet.

HTML markup
<div role="radiogroup" aria-labelledby="r3-label">
  <h4 id="r3-label">Which is your favourite pet?</h4>
  <p class="error">Error: Choose one pet.</p>
  <div>
    <input type="radio" id="r3-dog" name="r3" value="dog">
    <label for="r3-dog">Dog</label>
  </div>
  <div>
    <input type="radio" id="r3-cat" name="r3" value="cat">
    <label for="r3-cat">Cat</label>
  </div>
  <div>
    <input type="radio" id="r3-rabbit" name="r3" value="rabbit">
    <label for="r3-rabbit">Rabbit</label>
  </div>
</div>

2. aria-invalid on the group

aria-invalid marks the group as being in error. It is supported on radiogroup, not on the group role.

Example 2.1: On the fieldset

Poor practice: aria-invalid is deprecated as a global attribute in ARIA 1.2 and is not supported on the group role.

Which is your favourite pet?

Error: Choose one pet.

HTML markup
<fieldset aria-invalid="true">
  <legend>Which is your favourite pet?</legend>
  <p class="error">Error: Choose one pet.</p>
  <div>
    <input type="radio" id="r4-dog" name="r4" value="dog">
    <label for="r4-dog">Dog</label>
  </div>
  <div>
    <input type="radio" id="r4-cat" name="r4" value="cat">
    <label for="r4-cat">Cat</label>
  </div>
  <div>
    <input type="radio" id="r4-rabbit" name="r4" value="rabbit">
    <label for="r4-rabbit">Rabbit</label>
  </div>
</fieldset>

Example 2.2: On the div with role="group"

Poor practice: aria-invalid is deprecated as a global attribute in ARIA 1.2 and is not supported on the group role.

Which is your favourite pet?

Error: Choose one pet.

HTML markup
<div role="group" aria-labelledby="r5-label" aria-invalid="true">
  <h4 id="r5-label">Which is your favourite pet?</h4>
  <p class="error">Error: Choose one pet.</p>
  <div>
    <input type="radio" id="r5-dog" name="r5" value="dog">
    <label for="r5-dog">Dog</label>
  </div>
  <div>
    <input type="radio" id="r5-cat" name="r5" value="cat">
    <label for="r5-cat">Cat</label>
  </div>
  <div>
    <input type="radio" id="r5-rabbit" name="r5" value="rabbit">
    <label for="r5-rabbit">Rabbit</label>
  </div>
</div>

Example 2.3: On the div with role="radiogroup"

Acceptable: aria-invalid is supported on radiogroup, so the group exposes an invalid state.

Which is your favourite pet?

Error: Choose one pet.

HTML markup
<div role="radiogroup" aria-labelledby="r6-label" aria-invalid="true">
  <h4 id="r6-label">Which is your favourite pet?</h4>
  <p class="error">Error: Choose one pet.</p>
  <div>
    <input type="radio" id="r6-dog" name="r6" value="dog">
    <label for="r6-dog">Dog</label>
  </div>
  <div>
    <input type="radio" id="r6-cat" name="r6" value="cat">
    <label for="r6-cat">Cat</label>
  </div>
  <div>
    <input type="radio" id="r6-rabbit" name="r6" value="rabbit">
    <label for="r6-rabbit">Rabbit</label>
  </div>
</div>

3. aria-invalid on the radios

aria-invalid on the radios themselves.

None of these options is good practice.

Example 3.1: aria-invalid on each radio

Poor practice: aria-invalid is not supported on the radio role. Every radio is marked invalid.

Which is your favourite pet?

Error: Choose one pet.

HTML markup
<fieldset>
  <legend>Which is your favourite pet?</legend>
  <p class="error">Error: Choose one pet.</p>
  <div>
    <input type="radio" id="r7-dog" name="r7" value="dog" aria-invalid="true">
    <label for="r7-dog">Dog</label>
  </div>
  <div>
    <input type="radio" id="r7-cat" name="r7" value="cat" aria-invalid="true">
    <label for="r7-cat">Cat</label>
  </div>
  <div>
    <input type="radio" id="r7-rabbit" name="r7" value="rabbit" aria-invalid="true">
    <label for="r7-rabbit">Rabbit</label>
  </div>
</fieldset>

Example 3.2: aria-invalid on one radio only

Poor practice: aria-invalid is not supported on the radio role. Only that radio is marked invalid, so users may think only that radio is in error.

Which is your favourite pet?

Error: Choose one pet.

HTML markup
<fieldset>
  <legend>Which is your favourite pet?</legend>
  <p class="error">Error: Choose one pet.</p>
  <div>
    <input type="radio" id="r8-dog" name="r8" value="dog" aria-invalid="true">
    <label for="r8-dog">Dog</label>
  </div>
  <div>
    <input type="radio" id="r8-cat" name="r8" value="cat">
    <label for="r8-cat">Cat</label>
  </div>
  <div>
    <input type="radio" id="r8-rabbit" name="r8" value="rabbit">
    <label for="r8-rabbit">Rabbit</label>
  </div>
</fieldset>

The same example as 2.3, repeated here so it is easy to find.

Example 4.1: On the div with role="radiogroup"

Acceptable: aria-invalid is supported on radiogroup, so the group exposes an invalid state.

Which is your favourite pet?

Error: Choose one pet.

HTML markup
<div role="radiogroup" aria-labelledby="r9-label" aria-invalid="true">
  <h4 id="r9-label">Which is your favourite pet?</h4>
  <p class="error">Error: Choose one pet.</p>
  <div>
    <input type="radio" id="r9-dog" name="r9" value="dog">
    <label for="r9-dog">Dog</label>
  </div>
  <div>
    <input type="radio" id="r9-cat" name="r9" value="cat">
    <label for="r9-cat">Cat</label>
  </div>
  <div>
    <input type="radio" id="r9-rabbit" name="r9" value="rabbit">
    <label for="r9-rabbit">Rabbit</label>
  </div>
</div>