Sub topic 8
Input types and autocomplete
Earlier chapters covered labels, hints, field order and grouping. This chapter covers two attributes that decide how much typing a field takes: type and autocomplete. It assumes you can write basic HTML forms and read a short JavaScript snippet. It also assumes you know that every control needs an associated label.
What these attributes do
The type attribute on an <input> changes four things:
- The keyboard a phone shows. An email field gets an
@key, and a phone field gets a number pad. - The built-in validation the browser runs. For example,
type="email"rejects text with no@. - The semantics exposed to assistive technology.
- The value the field gives your script.
The autocomplete attribute tells the browser and password managers what a field is for. They can then fill it with saved data. It also satisfies WCAG 2.1 success criterion 1.3.5, Identify Input Purpose (level AA). This criterion applies to fields that collect information about the user.
Both attributes cost nothing to add. Both are easy to get wrong in ways that make a form worse.
Choosing an input type
Pick the type that matches the data, not the type that looks closest.
| Data | Use | Notes |
|---|---|---|
| Email address | `type="email"` | Checks for an `@` and basic structure |
| Phone number | `type="tel"` | Shows a phone keypad. Does not validate format |
| Web address | `type="url"` | Requires a scheme such as `https:` |
| Search query | `type="search"` | Some browsers add a clear button |
| Password | `type="password"` | Masks the characters |
| Quantity | `type="number"` | Only for values you would add or subtract |
| Date | `type="date"` | Browser-native picker. The interface differs by browser |
| Free text | `type="text"` | The default |
Why `type="number"` is often wrong
type="number" is meant for quantities. It causes these problems when used for identifiers:
- Browsers show spinner buttons. Scrolling the mouse wheel over a focused field can change the value.
- Leading zeros can be lost in the value you read back, so
01234can become1234. Zip codes and some postcodes can start with zero. - Spaces and hyphens are rejected. Card numbers and phone numbers are often written with them.
- Very long digit strings can lose precision when parsed as numbers.
- Screen reader support for the spin-button role is inconsistent.
For a postcode, card number or one-time code, use type="text" with inputmode="numeric".
<label for="quantity">Number of tickets</label>
<input id="quantity" name="quantity" type="number" min="1" max="10" step="1" value="1">
<label for="otp">Verification code</label>
<input id="otp" name="otp" type="text" inputmode="numeric"
autocomplete="one-time-code" pattern="[0-9]{6}" maxlength="6">The first field is a real quantity, so type="number" is right. The second is an identifier, so it uses text. The pattern attribute is a regular expression the whole value must match. It is a backstop, and you still need a clear error message when it fails.
inputmode and enterkeyhint
inputmode changes only the on-screen keyboard. It does not change validation or the value. Common values are numeric, decimal, tel, email, url and search. Use inputmode="decimal" for amounts that need a decimal point.
enterkeyhint changes the label on the keyboard's action key. The values include next, done, go, search and send. Use next on fields in the middle of a form and done or go on the last one.
What type="email" does and does not check
The browser's email check is loose on purpose. It accepts name@example with no top-level domain, because that is technically a valid address. It will not catch typos such as [email protected].
Three practical points follow:
- Keep
type="email". You get the right keyboard and a first check. - Do not write a stricter regular expression to reject unusual addresses. You will block valid people.
- The only reliable check is to send a confirmation message.
For email, also turn off the aggressive text helpers that phones apply:
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email"
autocapitalize="none" spellcheck="false" required>autocapitalize="none" stops the first letter being capitalized. spellcheck="false" stops red underlines under names and addresses.
Autocomplete tokens
The value of autocomplete is a standard token from the HTML specification. You can't invent your own. These are the ones you will use most.
| Field | Token |
|---|---|
| Full name | `name` |
| First name | `given-name` |
| Last name | `family-name` |
| `email` | |
| Phone number | `tel` |
| Username | `username` |
| Existing password | `current-password` |
| New password | `new-password` |
| One-time code | `one-time-code` |
| Street address (one multi-line field) | `street-address` |
| Address line 1, line 2 | `address-line1`, `address-line2` |
| City | `address-level2` |
| State or region | `address-level1` |
| Postal code | `postal-code` |
| Country | `country` or `country-name` |
| Card number | `cc-number` |
| Card expiry | `cc-exp` |
| Card security code | `cc-csc` |
| Name on card | `cc-name` |
The address-level tokens are numbered from the most specific level up, and their meaning varies by country. In the United States, address-level1 is the state and address-level2 is the city.
Shipping and billing
When one page has two addresses, prefix the token with shipping or billing. The browser can then fill each set from the right saved address.
<fieldset>
<legend>Billing address</legend>
<label for="bill-street">Street address</label>
<input id="bill-street" name="billing_street" type="text"
autocomplete="billing address-line1">
<label for="bill-postcode">Postcode</label>
<input id="bill-postcode" name="billing_postcode" type="text"
autocomplete="billing postal-code">
</fieldset>Passwords
Use current-password on sign-in forms and new-password on registration and password-change forms. Password managers use these tokens to decide whether to fill a saved password or offer to generate one. Keep the username token on the account field so the manager can pair the two.
Do not block pasting into password fields. Pasting is how password manager users sign in.
Putting it together
This sign-up fragment combines labels, types, input modes and tokens:
<form action="/signup" method="post">
<div>
<label for="given-name">First name</label>
<input id="given-name" name="given_name" type="text"
autocomplete="given-name" required>
</div>
<div>
<label for="family-name">Last name</label>
<input id="family-name" name="family_name" type="text"
autocomplete="family-name" required>
</div>
<div>
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email"
autocapitalize="none" spellcheck="false" required>
</div>
<div>
<label for="phone">Phone number (optional)</label>
<input id="phone" name="phone" type="tel" autocomplete="tel"
enterkeyhint="next">
</div>
<div>
<label for="new-password">Create a password</label>
<input id="new-password" name="password" type="password"
autocomplete="new-password" minlength="12" required>
</div>
<button type="submit">Create account</button>
</form>Splitting the name into two fields is a design decision. A single name field suits international users better, because not everyone has a first and last name. If your system really needs the parts, ask for them separately.
Common mistakes
- Using
type="number"for postcodes, card numbers or phone numbers. Use text withinputmode, ortype="tel"for phones. - Assuming
type="tel"validates. It does not. Validate in your own code or withpattern, and accept spaces, brackets and a leading+. - Setting
autocompleteon the wrong element or with a made-up value. An invalid token is ignored. - Relying on
patternwithout telling the user the format. Put the format in hint text, as covered earlier, and name it in the error message. - Using placeholder text as the format guide. The placeholder disappears when the user types.
- Trusting
typefor security. Types are conveniences in the browser. Validate every value again on the server.
Check the result
Test the types and tokens directly:
- Open the form on a phone, or in a browser's device emulator, and confirm each field shows the expected keyboard.
- Reload with saved browser data and confirm the fields fill. If a field doesn't fill, check its token against the table.
- Type
01234into any field you treat as an identifier. Submit it and confirm the leading zero survives. - Try a password manager on the sign-in and registration forms. Confirm it offers to fill and to generate as appropriate.
Exercise
A checkout form asks for a postcode with <input type="number" name="postcode"> and a card number with <input type="number" name="card">. Name two problems this causes. Then write the corrected markup for the postcode field, including its label and an autocomplete token.
Show answer
Problems: (1) type="number" can drop leading zeros from postcodes that begin with 0, and it rejects spaces or hyphens used in postcodes and card numbers; (2) it adds spinner buttons and mouse-wheel changes, and long card numbers can lose precision when parsed as numbers. Corrected postcode field:
<label for="postcode">Postcode</label>
<input id="postcode" name="postcode" type="text" autocomplete="postal-code">
If the postcode is digits only, you can also add inputmode="numeric". The card field should likewise be type="text" with inputmode="numeric" and autocomplete="cc-number".
Exercise
A registration page uses autocomplete="off" on the password field to stop browsers saving it. What is wrong with this, and what should the password field use instead?
Show answer
Most browsers and password managers ignore off on login-related fields, and where it works it stops people using a password manager, which harms both usability and security. On a registration form the field should use autocomplete="new-password" so managers can offer a strong generated password. A sign-in form should use current-password, paired with autocomplete="username" on the account field.