Testing hints and errors on radio groups

Hints and error messages for a group of radios need to be programmatically associated with the group, not just placed near it. Hints and errors use the same method, so they are tested together. Every example is pre-set in the markup, so no JavaScript is needed.

Simplest approach: link the hint or error to the group with aria-describedby. 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 with no hint or error shown.

Example 1.1: fieldset

Control: A group with no hint or error shown.

Which is your favourite pet?
HTML markup
<fieldset>
  <legend>Which is your favourite pet?</legend>
  <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: A group with no hint or error shown.

Which is your favourite pet?

HTML markup
<div role="group" aria-labelledby="r2-label">
  <h4 id="r2-label">Which is your favourite pet?</h4>
  <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: A group with no hint or error shown.

Which is your favourite pet?

HTML markup
<div role="radiogroup" aria-labelledby="r3-label">
  <h4 id="r3-label">Which is your favourite pet?</h4>
  <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. Text with no association

The hint or error is visible, but nothing links it to the group.

None of these options is good practice.

Example 2.1: Hint, not linked

Poor practice: The hint is in a paragraph inside the group. Nothing associates it with the group, so assistive technology users may not hear it.

Which is your favourite pet?

Choose one pet.

HTML markup
<fieldset>
  <legend>Which is your favourite pet?</legend>
  <p>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: Error, not linked

Poor practice: The error is in a paragraph inside the group. Nothing associates it with the group, so assistive technology users may not hear it.

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="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>
</fieldset>

3. Error as text in the group's name

The error message is written into the group's name: the legend, or the labelling element.

None of these options is good practice.

Example 3.1: fieldset

Poor practice: The error is part of the group's accessible name, so it is announced when users enter the group. But it makes the name longer and changes it whenever there is an error. Prefer aria-describedby.

Which is your favourite pet? Error: Choose one pet.
HTML markup
<fieldset>
  <legend>Which is your favourite pet? <span class="error">Error: Choose one pet.</span></legend>
  <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>
</fieldset>

Example 3.2: div with role="group"

Poor practice: The error is part of the group's accessible name, so it is announced when users enter the group. But it makes the name longer and changes it whenever there is an error. Prefer aria-describedby.

Which is your favourite pet? Error: Choose one pet.

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

Example 3.3: div with role="radiogroup"

Poor practice: The error is part of the group's accessible name, so it is announced when users enter the group. But it makes the name longer and changes it whenever there is an error. Prefer aria-describedby.

Which is your favourite pet? Error: Choose one pet.

HTML markup
<div role="radiogroup" aria-labelledby="r8-label">
  <h4 id="r8-label">Which is your favourite pet? <span class="error">Error: Choose one pet.</span></h4>
  <div>
    <input type="radio" id="r8-dog" name="r8" value="dog">
    <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>
</div>

4. Hint linked to the group

The hint is linked to the group with aria-describedby.

Example 4.1: fieldset

Acceptable: The hint is associated with the group with aria-describedby, so assistive technology can announce it as the group's description.

Which is your favourite pet?

Choose one pet.

HTML markup
<fieldset aria-describedby="r9-hint">
  <legend>Which is your favourite pet?</legend>
  <p id="r9-hint">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>
</fieldset>

Example 4.2: div with role="group"

Acceptable: The hint is associated with the group with aria-describedby, so assistive technology can announce it as the group's description.

Which is your favourite pet?

Choose one pet.

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

Example 4.3: div with role="radiogroup"

Acceptable: The hint is associated with the group with aria-describedby, so assistive technology can announce it as the group's description.

Which is your favourite pet?

Choose one pet.

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

5. Error linked to the group

The error is linked to the group with aria-describedby.

Example 5.1: fieldset

Acceptable: The error is associated with the group with aria-describedby, so assistive technology can announce it as the group's description.

Which is your favourite pet?

Error: Choose one pet.

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

Example 5.2: div with role="group"

Acceptable: The error is associated with the group with aria-describedby, so assistive technology can announce it as the group's description.

Which is your favourite pet?

Error: Choose one pet.

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

Example 5.3: div with role="radiogroup"

Acceptable: The error is associated with the group with aria-describedby, so assistive technology can announce it as the group's description.

Which is your favourite pet?

Error: Choose one pet.

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

6. Hint and error linked to the group

One aria-describedby lists both IDs, separated by a space.

Example 6.1: fieldset

Acceptable: The hint and error are associated with the group with aria-describedby, so assistive technology can announce them as the group's description.

Which is your favourite pet?

Choose one pet.

Error: Choose one pet.

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

Example 6.2: div with role="group"

Acceptable: The hint and error are associated with the group with aria-describedby, so assistive technology can announce them as the group's description.

Which is your favourite pet?

Choose one pet.

Error: Choose one pet.

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

Example 6.3: div with role="radiogroup"

Acceptable: The hint and error are associated with the group with aria-describedby, so assistive technology can announce them as the group's description.

Which is your favourite pet?

Choose one pet.

Error: Choose one pet.

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

7. aria-describedby on the radios

The hint or error is linked to the radios themselves.

None of these options is good practice.

Example 7.1: aria-describedby on each radio

Poor practice: The error is repeated on every radio, and it is not associated with the group itself.

Which is your favourite pet?

Error: Choose one pet.

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

Example 7.2: aria-describedby on one radio only

Poor practice: Only that radio has the error description, so users may think only that radio is in error, not the group.

Which is your favourite pet?

Error: Choose one pet.

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

The same examples as section 6, repeated here so they are easy to find.

Example 8.1: fieldset

Acceptable: The hint and error are associated with the group with aria-describedby, so assistive technology can announce them as the group's description.

Which is your favourite pet?

Choose one pet.

Error: Choose one pet.

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

Example 8.2: div with role="group"

Acceptable: The hint and error are associated with the group with aria-describedby, so assistive technology can announce them as the group's description.

Which is your favourite pet?

Choose one pet.

Error: Choose one pet.

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

Example 8.3: div with role="radiogroup"

Acceptable: The hint and error are associated with the group with aria-describedby, so assistive technology can announce them as the group's description.

Which is your favourite pet?

Choose one pet.

Error: Choose one pet.

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