Testing aria-invalid on checkbox groups
When a group of checkboxes 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 each checkbox. 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.
HTML markup
<fieldset>
<legend>Which pets do you have?</legend>
<p class="error">Error: Choose at least one pet.</p>
<div>
<input type="checkbox" id="c1-dog" name="c1" value="dog">
<label for="c1-dog">Dog</label>
</div>
<div>
<input type="checkbox" id="c1-cat" name="c1" value="cat">
<label for="c1-cat">Cat</label>
</div>
<div>
<input type="checkbox" id="c1-rabbit" name="c1" value="rabbit">
<label for="c1-rabbit">Rabbit</label>
</div>
</fieldset>
Example 1.2: div with role="group"
Control: The group with the error text shown and no invalid state.
HTML markup
<div role="group" aria-labelledby="c2-label">
<h4 id="c2-label">Which pets do you have?</h4>
<p class="error">Error: Choose at least one pet.</p>
<div>
<input type="checkbox" id="c2-dog" name="c2" value="dog">
<label for="c2-dog">Dog</label>
</div>
<div>
<input type="checkbox" id="c2-cat" name="c2" value="cat">
<label for="c2-cat">Cat</label>
</div>
<div>
<input type="checkbox" id="c2-rabbit" name="c2" value="rabbit">
<label for="c2-rabbit">Rabbit</label>
</div>
</div>
2. aria-invalid on the group
aria-invalid marks the group as being in error. It is deprecated as a global attribute in ARIA 1.2 and is not supported on the group role, which both fieldset and div with role="group" have.
None of these options is good practice.
Example 2.1: On the fieldset
Poor practice: The group role does not support aria-invalid.
HTML markup
<fieldset aria-invalid="true">
<legend>Which pets do you have?</legend>
<p class="error">Error: Choose at least one pet.</p>
<div>
<input type="checkbox" id="c3-dog" name="c3" value="dog">
<label for="c3-dog">Dog</label>
</div>
<div>
<input type="checkbox" id="c3-cat" name="c3" value="cat">
<label for="c3-cat">Cat</label>
</div>
<div>
<input type="checkbox" id="c3-rabbit" name="c3" value="rabbit">
<label for="c3-rabbit">Rabbit</label>
</div>
</fieldset>
Example 2.2: On the div with role="group"
Poor practice: The group role does not support aria-invalid.
HTML markup
<div role="group" aria-labelledby="c4-label" aria-invalid="true">
<h4 id="c4-label">Which pets do you have?</h4>
<p class="error">Error: Choose at least one pet.</p>
<div>
<input type="checkbox" id="c4-dog" name="c4" value="dog">
<label for="c4-dog">Dog</label>
</div>
<div>
<input type="checkbox" id="c4-cat" name="c4" value="cat">
<label for="c4-cat">Cat</label>
</div>
<div>
<input type="checkbox" id="c4-rabbit" name="c4" value="rabbit">
<label for="c4-rabbit">Rabbit</label>
</div>
</div>
3. aria-invalid on the checkboxes
aria-invalid is supported on the checkbox role.
Example 3.1: aria-invalid on each checkbox
Acceptable: aria-invalid is supported on checkbox, so each checkbox exposes an invalid state.
HTML markup
<fieldset>
<legend>Which pets do you have?</legend>
<p class="error">Error: Choose at least one pet.</p>
<div>
<input type="checkbox" id="c5-dog" name="c5" value="dog" aria-invalid="true">
<label for="c5-dog">Dog</label>
</div>
<div>
<input type="checkbox" id="c5-cat" name="c5" value="cat" aria-invalid="true">
<label for="c5-cat">Cat</label>
</div>
<div>
<input type="checkbox" id="c5-rabbit" name="c5" value="rabbit" aria-invalid="true">
<label for="c5-rabbit">Rabbit</label>
</div>
</fieldset>
Example 3.2: aria-invalid on one checkbox only
Poor practice: Only that checkbox is marked invalid, so users may think only that checkbox is in error, when the error is about the group.
HTML markup
<fieldset>
<legend>Which pets do you have?</legend>
<p class="error">Error: Choose at least one pet.</p>
<div>
<input type="checkbox" id="c6-dog" name="c6" value="dog" aria-invalid="true">
<label for="c6-dog">Dog</label>
</div>
<div>
<input type="checkbox" id="c6-cat" name="c6" value="cat">
<label for="c6-cat">Cat</label>
</div>
<div>
<input type="checkbox" id="c6-rabbit" name="c6" value="rabbit">
<label for="c6-rabbit">Rabbit</label>
</div>
</fieldset>
4. Recommended practice
The same example as 3.1, repeated here so it is easy to find.
Example 4.1: aria-invalid on each checkbox
Acceptable: aria-invalid is supported on checkbox, so each checkbox exposes an invalid state.
HTML markup
<fieldset>
<legend>Which pets do you have?</legend>
<p class="error">Error: Choose at least one pet.</p>
<div>
<input type="checkbox" id="c7-dog" name="c7" value="dog" aria-invalid="true">
<label for="c7-dog">Dog</label>
</div>
<div>
<input type="checkbox" id="c7-cat" name="c7" value="cat" aria-invalid="true">
<label for="c7-cat">Cat</label>
</div>
<div>
<input type="checkbox" id="c7-rabbit" name="c7" value="rabbit" aria-invalid="true">
<label for="c7-rabbit">Rabbit</label>
</div>
</fieldset>