Accessible Forms
A form that some people can't complete is a locked door. Blind users rely on screen readers, others use only a keyboard, and many people have low vision, shaky hands or are simply in a hurry on a small phone. Accessible forms work for all of them, and the techniques are mostly plain HTML.
1. Every control needs a label
A visible <label> connected to its control is the single most important rule. There are two ways to connect them:
<!-- Explicit: for matches the id --> <label for="phone">Phone number</label> <input type="tel" id="phone" name="phone"> <!-- Implicit: the input sits inside the label --> <label> <input type="checkbox" name="sms"> Send me SMS reminders </label>
for and id links the words to the control, for screen readers and for mouse and touch users. Exact announcements vary by screen reader.The connection gives three benefits: screen readers announce the label when the control gets focus, voice-control users can say "click Phone number", and tapping the label focuses the control, which makes small checkboxes much easier to hit on a phone.
Writing the label as plain text, such as <p>Phone number</p>, next to the input. It looks the same, but a screen reader just announces "edit text" with no idea what to type. Also remember that ids must be unique: two inputs with id="phone" break the connection.
2. Group related controls with fieldset and legend
For radio buttons and checkbox groups, each option has its own label, but the question needs one too. Wrap the group in <fieldset> and put the question in a <legend> as its first child.
<fieldset> <legend>How would you like to pay?</legend> <label><input type="radio" name="pay" value="mpesa"> M-Pesa</label> <label><input type="radio" name="pay" value="card"> Card</label> <label><input type="radio" name="pay" value="cash"> Cash on delivery</label> </fieldset>
When focus enters the group, screen readers announce the legend along with the option, for example "How would you like to pay? M-Pesa, radio button, 1 of 3". Browsers draw a border around a fieldset with the legend set into it, which CSS can restyle.
3. Give hints and say what's required
If a field needs a particular format, say so before the user types, in text connected to the field with aria-describedby. The screen reader reads the label first, then the description.
<label for="id-no">National ID number (required)</label>
<input type="text" id="id-no" name="id_no" inputmode="numeric" required
aria-describedby="id-hint">
<p id="id-hint">7 or 8 digits, as printed on the front of your ID card.</p>
- Mark required fields in words, such as "(required)", not only with a red asterisk or colour. The
requiredattribute also tells screen readers. inputmode="numeric"shows a number keyboard on phones without the problems oftype="number".
4. Help people fix errors
Good error messages are specific, polite and placed next to the problem. "Invalid input" helps nobody; "Enter a phone number with 10 digits, like 0712345678" does. When you show your own error messages (usually with JavaScript or from the server):
- Put the message near the field and connect it with
aria-describedby. - Mark the field with
aria-invalid="true"so screen readers announce it as invalid. - Don't rely on colour alone: add an icon or the word "Error", because some users can't distinguish red from green.
- Keep what the user already typed. Making people re-enter everything is frustrating.
<label for="email">Email</label>
<input type="email" id="email" name="email" value="amina.example.com"
aria-invalid="true" aria-describedby="email-error">
<p id="email-error">Error: the email address needs an @, like amina@example.com.</p>
5. Make it work with a keyboard and autofill
- Use real form controls and
<button>elements; they work with Tab, Space and Enter automatically. A clickable<div>does not. - Keep controls in a logical order in the HTML, so Tab moves through them as expected.
- Never remove the focus outline in CSS without replacing it with another clear focus style.
- Add
autocompletevalues such asname,email,telandstreet-address. Browsers can then fill fields automatically, a big help for people with motor or memory difficulties.
Try it
Use only the keyboard: press Tab to move, Space to tick or select, and arrow keys to change the radio option.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Accessible form</title>
<style>
label { display: block; margin-top: 10px; }
fieldset label { display: inline; margin-right: 12px; }
.hint { color: #555; font-size: 0.9em; margin: 4px 0 0; }
</style>
</head>
<body>
<h1>Bursary application</h1>
<form>
<label for="fullname">Full name (required)</label>
<input type="text" id="fullname" name="fullname" autocomplete="name" required>
<label for="phone">Phone number (required)</label>
<input type="tel" id="phone" name="phone" autocomplete="tel" required
aria-describedby="phone-hint">
<p class="hint" id="phone-hint">10 digits, for example 0712345678.</p>
<fieldset>
<legend>Level of study</legend>
<label><input type="radio" name="level" value="college" checked> College</label>
<label><input type="radio" name="level" value="university"> University</label>
<label><input type="radio" name="level" value="tvet"> TVET</label>
</fieldset>
<label><input type="checkbox" name="confirm" required> I confirm the details are true</label>
<p><button type="submit">Submit application</button></p>
</form>
</body>
</html>
Each label sits above its box, the phone hint appears in smaller grey text under the phone field, and the three study levels are grouped inside a bordered box titled "Level of study". Clicking any label text focuses or selects its control. If you submit with empty required fields, the browser shows its validation message; with valid values the browser tries to send the form, and the preview may go blank because there is no server; press Run to start again.
Test with a real screen reader. NVDA is free for Windows, VoiceOver is built into iPhones and Macs, and TalkBack is built into Android phones. Five minutes of listening to your own form teaches more than any checklist.
Key points
- Every control needs a visible
<label>, linked withfor/idor by wrapping; placeholders are not labels. - Group radio buttons and checkboxes in a
<fieldset>with a<legend>. - Connect hints and error messages with
aria-describedby, and mark errors witharia-invalidand words, not colour alone. - Use real controls, a logical order, visible focus and
autocompletevalues.
Tip: use the ← and → keys to move between pages.
