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:
- the screen reader announces "Full name, edit text, required" when reaching the field;
- clicking the label puts the cursor in the field, which enlarges the clickable area on mobile;
- 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:
| Type | Use | Effect |
|---|---|---|
email | e-mail address | keyboard with @, format checked |
tel | phone | numeric keypad, no format imposed |
number | quantity | arrows, min, max, step |
date | date | the system's date picker |
password | password | hidden characters |
search | search | "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>
requiredmakes the field mandatory;min,maxandstepframe a number or a date;patternimposes a regular expression, that is, a pattern: here, exactly 6 letters or digits;aria-describedbylinks 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.
Text description of the animation
- The user submits the form.
- The browser checks each field's constraints.
- A field is invalid: sending is blocked, focus goes to that field and a message is displayed.
- After correction, all the constraints are met.
- The browser sends the data, each piece under the name given by its name attribute.
- 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
<input type="checkbox" required>, with its label.Hint 2
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: eachformust point to a uniqueid, otherwise the label attaches to the wrong field. - Using
type="number"for the phone: spaces and+are refused, and pointless arrows appear.
Summary
- 1Each field has a <label for> matching its id.
- 2The field type (email, tel, number, date…) gives the right keyboard and free checks.
- 3required, min, max and pattern have the browser validate input.
- 4Browser validation helps the user; the server must always check everything again.
- 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.