Skip to content
KALANBLON
Show contents

Chapter 2 of 7 · 20 min

Accessible forms

By the end of this chapter, you'll be able to

  • Associate each field with its label using label and for
  • Choose the right field type to get the right keyboard on mobile
  • Have the browser validate input with required, min, max and pattern
  • Group related choices with fieldset and legend

One field, one label

Each field (<input>, <select>, <textarea>) needs a <label>. The label's for attribute must have the same value as the field's id:

<form action="/appointment" method="post">
  <label for="name">Full name</label>
  <input id="name" name="name" type="text" autocomplete="name" required>

  <label for="phone">Phone</label>
  <input id="phone" name="phone" type="tel" autocomplete="tel" required>

  <label for="date">Preferred date</label>
  <input id="date" name="date" type="date" min="2026-01-01" required>

  <button type="submit">Book an appointment</button>
</form>

This association has three effects:

  1. the screen reader announces "Full name, edit text, required" when reaching the field;
  2. clicking the label puts the cursor in the field, which enlarges the clickable area on mobile;
  3. accessibility testing tools check that the field has a name.

name is the name under which the value is sent to the server. autocomplete lets the browser suggest the name or number already saved.

The right field type

The type changes the keyboard on mobile and turns on automatic checks:

TypeUseEffect
emaile-mail addresskeyboard with @, format checked
telphonenumeric keypad, no format imposed
numberquantityarrows, min, max, step
datedatethe system's date picker
passwordpasswordhidden characters
searchsearch"Search" key on the keyboard

type="tel" imposes no format, because numbers vary between countries. For a quantity, number fits. For a postcode or a card number, prefer type="text" with inputmode="numeric": they aren't numbers you add up, and a leading zero must be kept.

Validation by the browser

Before even sending anything, the browser can check the input:

<label for="quantity">Number of seats (1 to 6)</label>
<input id="quantity" name="quantity" type="number" min="1" max="6" value="1" required>

<label for="code">Discount code (6 letters or digits, optional)</label>
<input id="code" name="code" type="text" pattern="[A-Za-z0-9]{6}" aria-describedby="code-help">
<p id="code-help">It's printed on your voucher, for example KALA26.</p>
  • required makes the field mandatory;
  • min, max and step frame a number or a date;
  • pattern imposes a regular expression, that is, a pattern: here, exactly 6 letters or digits;
  • aria-describedby links the field to a help text, read by the screen reader after the label.

The server checks everything again: browser validation helps the user, but anyone can get round it.

Step 6 of 6
Text description of the animation
  1. The user submits the form.
  2. The browser checks each field's constraints.
  3. A field is invalid: sending is blocked, focus goes to that field and a message is displayed.
  4. After correction, all the constraints are met.
  5. The browser sends the data, each piece under the name given by its name attribute.
  6. The server checks everything again: browser validation helps the user, but anyone can get round it.

Browser validation is a convenience for the user, not security: anyone can send data without going through your page. The server must always check everything again.

Grouping choices

Radio buttons or checkboxes answering the same question are grouped in a <fieldset>, whose <legend> is the question:

<fieldset>
  <legend>Payment method</legend>
  <input type="radio" id="pay-cash" name="payment" value="cash" required>
  <label for="pay-cash">Cash</label>
  <input type="radio" id="pay-mobile" name="payment" value="mobile">
  <label for="pay-mobile">Mobile payment</label>
</fieldset>

All the radio buttons in a group share the same name: only one can be chosen. The screen reader announces "Payment method, group" then each option.

Your turn

Fix this sign-up formHTML · CSS

Preview

The preview is isolated from the page and runs no scripts · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Worked exercise

Worked exercise: booking a car share

Write the form for booking a car share: departure town and arrival town (required), date of the journey (required), number of seats from 1 to 4, phone (required), and a required checkbox "I accept the car-sharing rules". Each field has a label, the right type and the right constraints.

Hint 1
A required checkbox: <input type="checkbox" required>, with its label.
Hint 2
For the number of seats: type="number" with min="1", max="4" and a default value.
Show the solution and common mistakes
<form action="/booking" method="post">
  <label for="from">Departure town</label>
  <input id="from" name="from" type="text" required>

  <label for="to">Arrival town</label>
  <input id="to" name="to" type="text" required>

  <label for="day">Date of the journey</label>
  <input id="day" name="day" type="date" required>

  <label for="seats">Number of seats (1 to 4)</label>
  <input id="seats" name="seats" type="number" min="1" max="4" value="1" required>

  <label for="phone">Phone</label>
  <input id="phone" name="phone" type="tel" autocomplete="tel" required>

  <input id="rules" name="rules" type="checkbox" required>
  <label for="rules">I accept the car-sharing rules</label>

  <button type="submit">Book</button>
</form>

Common mistakes:

  • Giving two fields the same id: each for must point to a unique id, otherwise the label attaches to the wrong field.
  • Using type="number" for the phone: spaces and + are refused, and pointless arrows appear.

Summary

  1. 1Each field has a <label for> matching its id.
  2. 2The field type (email, tel, number, date…) gives the right keyboard and free checks.
  3. 3required, min, max and pattern have the browser validate input.
  4. 4Browser validation helps the user; the server must always check everything again.
  5. 5A placeholder is never a label.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.