Sub topic 1
Anatomy of a Well-Built Form Field
A form field is more than an input control. It includes a label, instructions, current state, validation feedback, and the relationships that connect these parts. This chapter assumes you know basic HTML form elements, basic JavaScript or TypeScript, and how a browser submits form data.
A consistent field anatomy helps people scan a form, understand what each control expects, correct mistakes, and use assistive technology. Build this structure before deciding how fields are arranged into larger steps.
Prerequisites
You should be familiar with:
- The HTML elements
input,label,select,textarea, andbutton - The
nameattribute and how it becomes a submitted form value - Basic CSS layout
- The purpose of
id,for, and ARIA attributes
The parts of a field
A well-built field usually contains these parts:
| Part | Purpose | Example |
|---|---|---|
| Label | Names the value the user must provide | Email address |
| Control | Collects or displays the value | Text input |
| Hint | Explains format, purpose, or constraints | We only use this for receipts |
| Error | Explains what must be corrected | Enter a valid email address |
| Group | Connects related controls | Payment method |
The label should be visible whenever possible. Placeholder text is not a replacement for a label because it disappears when the user enters a value. It also provides weak contrast and can be difficult to revisit.
Connect the label to the control with matching for and id values. The control needs a useful name so its value can be included in the submitted data.
Hints should answer a question the user is likely to have. For example, explain why you need a phone number or show the required format. Avoid repeating the label or adding instructions that do not help the user complete the field.
Error text should be near the field and should describe the correction, not merely report a problem. Connect hints and errors with aria-describedby so a screen reader can find them when it focuses the control. Set aria-invalid="true" only when the current value is invalid.
<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 autocomplete token tells the browser what kind of information the field accepts. It can reduce typing and improve accuracy. Use the token that matches the meaning of the field, such as email, given-name, or postal-code.
Grouping related controls
Use a fieldset and legend when several controls answer one question. Radio buttons for a delivery method are one group, even though each radio button has its own label. The legend provides the shared question.
For a group of checkboxes, state whether users may select one option or several. Keep related controls close together and order them in the sequence people need to answer them. Do not use visual position alone to communicate relationships.
<fieldset>
<legend>How should we deliver your order?</legend>
<label>
<input type="radio" name="delivery" value="ship">
Ship it
</label>
<label>
<input type="radio" name="delivery" value="pickup">
Pick it up
</label>
</fieldset>A field's changing state
A field can be empty, filled, focused, touched, dirty, valid, or invalid. “Touched” means the user has interacted with it. “Dirty” means its value differs from the initial value. Keep these states separate because they support different decisions.
For example, you might avoid showing an error for an untouched field, show a hint while it has focus, and prevent the next step when it is invalid. The visual design should make focus, disabled, and invalid states clear without relying on color alone.
Exercise
Why should a placeholder not be the only label for an email field?
Show answer
It disappears after the user types, does not reliably identify the field while reviewing the form, and may have poor contrast. Use a visible label connected to the control.