Sub topic 1
Anatomy of a well-built form field
Prerequisites
This chapter assumes you can write basic HTML form elements (input, label, select, button) and read short TypeScript snippets. It also assumes you know that a browser submits a form as name and value pairs, using each control's name attribute. Nothing else is needed. Later chapters build on this one, so the field structure here appears again in validation, error messaging, and accessibility.
Why the single field matters
A multi-step flow is a sequence of fields. If each field is unclear, adding steps, conditions, and drafts only makes the problems harder to find. Most form defects start at the field level:
- A label that disappears when the user types.
- A hint that a screen reader never announces.
- An error message that sits near the input but is not linked to it.
- A keyboard that does not match the data being asked for.
A well-built field has six parts, and each does a separate job.
| Part | Job | Typical markup |
|---|---|---|
| Label | Names the field, always visible | `<label for="...">` |
| Control | Collects the value | `<input>`, `<select>`, `<textarea>` |
| Type and attributes | Pick the right keyboard, browser checks, and autofill | `type`, `autocomplete`, `inputmode`, `required` |
| Hint | Explains format or purpose before the user types | `<p id="...-hint">` |
| Error | Explains what to fix after a failure | `<p id="...-error">` |
| Links | Connect the parts so assistive technology announces them | `for`, `id`, `aria-describedby`, `aria-invalid` |
A complete field
This example shows all six parts for an email field. It shows the field in its error state.
<div>
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email" required
aria-describedby="email-hint email-error" aria-invalid="true">
<p id="email-hint">We only use this to send your receipt.</p>
<p id="email-error">Enter an email address in the format [email protected].</p>
</div>The next sections take each part in turn.
The label
The label is the field's name. It must be a real <label> element, connected to the control by matching the label's for attribute to the input's id. This connection gives you three things at once:
- A screen reader announces "Email address" when the input receives focus.
- Clicking or tapping the label moves focus to the input, which enlarges the touch target.
- Voice-control software can target the field by its visible name.
Wrapping the input inside the label also works, but explicit for and id pairs are the more reliable choice in component libraries, because they survive layout changes.
Two common mistakes weaken this:
- Using the placeholder as the label. A placeholder vanishes as soon as the user types. People then cannot check what a field asked for, and users with memory or attention difficulties lose context. Placeholder text also has low contrast by default. Use it, if at all, for an example value, never for the field's name.
- Using a
<div>or<span>styled like a label. It looks right but has no programmatic link to the input.
Write labels as short noun phrases that match the user's own words: "Email address", "Date of birth". Mark optional fields as "(optional)" in the label text. If most fields are required, this is clearer than adding asterisks to every required one.
Choosing the control and its attributes
Pick the control that fits the data, then add attributes that let the browser help.
typeselects the on-screen keyboard and built-in checks.type="email"shows an@key on mobile.type="tel"shows a number pad.type="url"andtype="search"also change the keyboard.autocompletetells the browser what the field means, so it can fill it. Use standard tokens such asemail,given-name,family-name,tel,postal-code,street-address,new-password,current-password, andone-time-code. Autofill saves typing and cuts errors, and it is also an accessibility requirement (WCAG 1.3.5, Identify Input Purpose) for fields that collect personal data.inputmodechanges only the keyboard, without changing validation.inputmode="numeric"is useful for values like a verification code or a postal code.requiredmarks a field the browser and assistive technology treat as mandatory.
Avoid type="number" for identifiers such as phone numbers, card numbers, or postal codes. It adds spinner buttons, drops leading zeros in some code paths, and can change a value when the user scrolls over it. Use type="text" with inputmode="numeric" and a suitable autocomplete token instead.
<label for="postcode">Postal code</label>
<input id="postcode" name="postcode" type="text" inputmode="numeric"
autocomplete="postal-code">Match the control's width to the expected value. A postal code field that spans the whole page suggests a long answer, while a short field signals the expected length without any extra text.
Hints
A hint answers the question "what do you want from me?" before the user makes a mistake. Good hints state the format ("Use the format DD/MM/YYYY"), the reason ("We only use this to send your receipt"), or a constraint ("At least 12 characters").
Put the hint in the page as text next to the field, linked with aria-describedby. Do not hide it in a tooltip that appears only on hover, because keyboard and touch users cannot reach it. Keep hints for information the user needs. A field whose label is "Email address" does not need a hint saying "Enter your email address".
Linking hint and error with aria-describedby
aria-describedby takes a space-separated list of element ids. A screen reader announces the label first, then the field's role and state, then the description made from the listed elements in order. In the example above, a user would hear the label, then the hint, then the error.
Two details cause bugs in practice:
- Every id in the list must exist. A reference to a missing element is silently ignored, so a typo makes the hint inaccessible without any visible symptom.
- An element that is hidden but still referenced is still read. An element referenced by
aria-describedbyis included in the description even when it is hidden withdisplay: none. If you keep an empty error paragraph in the markup and always reference it, the description is just the hint. But if a stale message stays in the paragraph, it will be announced as current. The safe pattern is to build the list from the elements that apply right now.
aria-invalid="true" tells assistive technology the current value failed validation. Set it only after a failure and remove it when the value is valid. A field marked invalid before the user has typed anything sounds like an accusation.
This helper keeps the three attributes consistent in one place:
function setFieldError(input: HTMLInputElement, message: string | null): void {
const hint = document.getElementById(`${input.id}-hint`);
const error = document.getElementById(`${input.id}-error`);
if (!error) return;
const ids: string[] = [];
if (hint) ids.push(hint.id);
if (message) {
error.textContent = message;
ids.push(error.id);
input.setAttribute("aria-invalid", "true");
} else {
error.textContent = "";
input.removeAttribute("aria-invalid");
}
input.setAttribute("aria-describedby", ids.join(" "));
}Calling setFieldError(input, "Enter your full name.") shows the message and links it. Calling setFieldError(input, null) clears the message, removes the invalid flag, and leaves only the hint in the description.
Order and placement of the parts
The reading order in the markup should match the visual order, because screen readers and keyboard focus follow the source. A common, reliable order is label, hint, control, error. The example earlier puts the hint and error after the input, which is also valid because aria-describedby does not depend on position. Choose one order and use it for every field, so users learn where to look. Keep the error message close to its input, ideally directly below it.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Screen reader says "edit text" with no name | Missing or unlinked label | Add `for` and a matching `id` |
| Autofill does not fill the field | No `autocomplete` token, or a made-up one | Use a standard token |
| Leading zeros disappear from a code | `type="number"` used for an identifier | Use `type="text"` with `inputmode="numeric"` |
| Error is displayed but never announced | Error text not in `aria-describedby` | Add its id to the list |
| Old error read after correction | Message text left in a referenced element | Clear the text and remove the id |
| Two fields share one label | Duplicate `id` values | Make every `id` unique on the page |
Duplicate ids deserve special attention in multi-step flows. If you render each step from a template, or keep earlier steps mounted but hidden, the same id can appear twice, and for and aria-describedby attach to the first match. Generate ids from the field name or a unique counter.
Exercise
A signup form has an input with the placeholder "Phone number" and no label element. The input uses type="number". Name three problems with this field and give one fix for each.
Show answer
First, the placeholder is the only name, so it disappears when the user types and a screen reader may not announce it reliably. Fix: add a visible <label for="phone"> linked to the input's id. Second, type="number" adds spinners, can strip leading zeros, and is wrong for an identifier. Fix: use type="tel" (or type="text" with inputmode="numeric"). Third, there is no autocomplete token, so the browser cannot autofill. Fix: add autocomplete="tel".
Exercise
An error paragraph with id "name-error" is always listed in the input's aria-describedby. After the user corrects the value, the code hides the paragraph with display: none but leaves its text in place. What does a screen reader announce, and how do you fix it?
Show answer
The description still includes the old error text, because an element referenced by aria-describedby is read even when hidden. Fix: clear the paragraph's text, remove "name-error" from aria-describedby, and remove aria-invalid, as the setFieldError helper does.