Lesson 7 of 23
In progress
Inputs, labels and helpful errors
Pick the right input type, label every field, group choices, and write error messages people can actually act on.
The input type is not a detail. It decides which keyboard appears on a phone, whether the browser offers autofill, and whether a value is checked before it reaches your server.
What you will learn
- Match input types to the answer you need.
- Label and group fields, including radio buttons and selects.
- Connect error messages to the field they describe.
Types that do real work
Editable. Runs in your browser.
<label for="age">Age</label>
<input id="age" name="age" type="number" min="13" max="120" step="1">
<label for="date">Start date</label>
<input id="date" name="date" type="date">
<label for="password">Password</label>
<input id="password" name="password" type="password" autocomplete="new-password" minlength="8">
<button type="submit">Continue</button>
</form>
Press Run to see the output.
type="email" gives a phone an email keyboard and rejects values without an @. type="number" shows a numeric keypad and enforces min, max and step. type="date" opens a date picker and submits an ISO date. type="password" masks the value, though it does not make the connection secure; HTTPS does that.
autocomplete is worth setting deliberately. email, name, tel, current-password and new-password let browsers and password managers fill the right field. Getting it wrong causes autofill to put a phone number in the email box. Correct autocomplete values also cut the typing a phone user has to do, which is a usability win rather than a convenience.
Search fields use type="search", telephone numbers use type="tel" (no validation, because formats vary), and URLs use type="url".
Choices: radio, checkbox, select
Checkboxes allow zero or more selections. Radio buttons allow exactly one from a group, and the grouping comes from sharing the same name.
Editable. Runs in your browser.
<label><input type="radio" name="source" value="search" checked> Search</label>
<label><input type="radio" name="source" value="friend"> A friend</label>
<label><input type="radio" name="source" value="social"> Social media</label>
</fieldset>
<label for="plan">Plan</label>
<select id="plan" name="plan">
<option value="free">Free</option>
<option value="pro">Pro</option>
</select>
Press Run to see the output.
The fieldset and legend turn three separate radios into one question. Without them, a screen reader user hears "Search, radio button" with no idea what is being asked. The value is what the server receives; the visible text is what the person reads, and they can differ.
A select is compact for long lists. For fewer than about five options, radios are faster to scan and require one click instead of two.
Text areas and hints
textarea is for multi-line answers. Its size is set with rows and cols, though CSS usually takes over.
Editable. Runs in your browser.
Press Run to see the output.
Hints belong in a paragraph tied to the field with aria-describedby, not in the placeholder. Placeholders vanish on input, and low-contrast placeholder text is a known accessibility problem.
Errors people can act on
When validation fails, say what is wrong and how to fix it, next to the field. "Invalid input" is not an error message; "Enter an email address, like name@example.com" is.
Editable. Runs in your browser.
Press Run to see the output.
aria-invalid="true" marks the field as failed, and aria-describedby makes the message part of the field’s announcement. role="alert" announces the message when it appears. Do not rely on a red border alone; colour is not a message.
Mark required fields in the label text as well as with required. An asterisk with no explanation is a puzzle; "Email (required)" is not.
Using a placeholder as the only label is the most common form accessibility failure. The hint disappears as soon as typing starts, so a person who looks away loses the question, and autofill has nothing to announce. Use a real label and keep the placeholder for an example value, if you use one at all.
Build a sign-up form with a name, an email, a password and a plan choice between Free and Pro. Label everything, group the plan with a fieldset, and add a hint under the password explaining the minimum length.
Show solution
Editable. Runs in your browser.
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email" required>
<label for="password">Password</label>
<input id="password" name="password" type="password"
autocomplete="new-password" minlength="8"
aria-describedby="password-hint" required>
<p id="password-hint">At least 8 characters.</p>
<fieldset>
<legend>Plan</legend>
<label><input type="radio" name="plan" value="free" checked> Free</label>
<label><input type="radio" name="plan" value="pro"> Pro</label>
</fieldset>
<button type="submit">Create account</button>
</form>
Press Run to see the output.
Each field has a visible label, the password has a persistent hint, and the radios share a name inside a labelled group. The minimum length is enforced by the browser and should also be checked on the server.
Recap
- Choose the input type that matches the answer; it improves keyboards, autofill and validation.
- Group radios with
fieldsetandlegend, and give every control aname. - Tie hints and errors to fields with
aria-describedby, and never use a placeholder as a label.
Next, you will structure whole pages with landmarks, so assistive technology can jump between regions.
Can I style native inputs?
What does autocomplete='off' actually do?
Progress is saved in this browser only. No account, no email.