KodingKaro
HTML · Intermediate · Advanced Forms

Fieldsets, legends, and accessible errors

Related controls need a shared explanation, and validation errors need to be understandable and connected to the affected field.

✓ What you will learn in this topic

  • Explain the purpose of Fieldsets, legends, and accessible errors in a real HTML document.
  • Choose and apply the elements shown in both examples.
  • Recognize the specific mistake described in this lesson.

Concept explanation

Fieldset groups related form controls; legend gives that group its name. For radio buttons and checkboxes, this shared context helps clarify what the choices answer. Error text should say what went wrong and how to fix it. Connect help or error text with aria-describedby and set aria-invalid when a submitted value is currently invalid. Do not rely on color alone or announce a generic error without identifying the field.

Syntax

Wrap a related group in fieldset and legend; connect visible help or error text using aria-describedby.

Example 1: a small page

The legend gives both radio options a shared question and each option has a clickable label.

<fieldset>
  <legend>Preferred contact</legend>
  <label><input type=\"radio\" name=\"contact\" value=\"email\"> Email</label>
  <label><input type=\"radio\" name=\"contact\" value=\"phone\"> Phone</label>
</fieldset>

Expected result: A named group presents two mutually exclusive contact choices.

Example 2: a practical page

The error is visible and programmatically associated with the invalid field.

<label for=\"email\">Email address</label>
<input id=\"email\" name=\"email\" type=\"email\" aria-invalid=\"true\" aria-describedby=\"email-error\" required>
<p id=\"email-error\">Enter an address such as name@example.com.</p>

Expected result: The field is announced as invalid with a specific correction hint.

Common mistakes

Client-side constraints improve usability but are not a security boundary or replacement for server validation.

Best practices

Use fieldset and legend for related controls, autocomplete tokens where appropriate, and connect error text to the control.

Practice exercise

Build an accessible event registration form with grouped ticket choices, an email field, an age range, and visible guidance for invalid values.

Lesson summary

  • Group related controls and tie useful error instructions directly to the affected field.
  • Use fieldset and legend for related controls, autocomplete tokens where appropriate, and connect error text to the control.

Knowledge check

Which element provides a shared title for controls inside a fieldset?

Show the explanation

Legend names the group so the shared context applies to each field in the fieldset.

Practical example

html
<fieldset>
  <legend>Preferred contact</legend>
  <label><input type=\"radio\" name=\"contact\" value=\"email\"> Email</label>
  <label><input type=\"radio\" name=\"contact\" value=\"phone\"> Phone</label>
</fieldset>

Expected result:

A named group presents two mutually exclusive contact choices.

Hands-on practice task

Build an accessible event registration form with grouped ticket choices, an email field, an age range, and visible guidance for invalid values.

In this section

Other lessons in Advanced Forms

Broaden your knowledge

Related learning topics