Sub topic 11
Autocomplete and input hints
This chapter assumes you can build a form with labeled text inputs and know how name and type work. It covers the attributes that help browsers and password managers fill fields, and the ones that hint at the right on-screen keyboard.
Why hints matter
Typing on a phone is slow and error-prone. Browsers can fill known values such as names, addresses, and one-time codes. They can also open a keyboard that fits the data. Both depend on attributes you add to the control. Without them, the browser guesses from the field's name, id, or label, and it often guesses wrong.
The autocomplete attribute
autocomplete tells the browser what kind of value a field holds. The value is a token from a fixed list, not free text.
<form action="/checkout" method="post">
<label for="fn">First name</label>
<input id="fn" name="first" autocomplete="given-name">
<label for="ln">Last name</label>
<input id="ln" name="last" autocomplete="family-name">
<label for="em">Email address</label>
<input id="em" name="email" type="email" autocomplete="email">
<label for="street">Street address</label>
<input id="street" name="street" autocomplete="street-address">
<label for="zip">Postal code</label>
<input id="zip" name="zip" autocomplete="postal-code">
<button type="submit">Continue</button>
</form>Common tokens:
| Token | Meaning |
|---|---|
| `name` | Full name |
| `email` | Email address |
| `tel` | Telephone number |
| `username` | Account name for sign-in |
| `current-password` | Existing password, used on sign-in forms |
| `new-password` | Password being created or changed |
| `one-time-code` | Code sent by SMS or an authenticator |
| `off` | Ask the browser not to fill or remember |
Use current-password on a sign-in form and new-password on a sign-up or change-password form. Password managers use the difference to decide whether to fill a saved password or offer to generate a new one.
Sections and prefixes
When one page collects two addresses, add a prefix so the browser keeps them apart. The tokens shipping and billing work as prefixes.
<input name="ship_zip" autocomplete="shipping postal-code">
<input name="bill_zip" autocomplete="billing postal-code">Keyboard hints: inputmode and enterkeyhint
inputmode selects the virtual keyboard without changing validation or the field's type. enterkeyhint changes the label of the Enter key on that keyboard.
<label for="code">Verification code</label>
<input id="code" name="code" inputmode="numeric"
autocomplete="one-time-code" enterkeyhint="done">Useful inputmode values are numeric, decimal, tel, email, url, and search. Prefer type="email" or type="tel" when the data really is an email or phone number. Use inputmode when the data is digits but not a number in the arithmetic sense, such as a postal code or a card number. type="number" suits those poorly because it allows spinners, strips leading zeros in some code paths, and accepts characters like e.
Placeholders are not labels
placeholder shows sample text inside an empty field. It disappears when the user types, it often has low contrast, and it is not a reliable accessible name. Keep a visible <label> and use the placeholder only for a format example.
<label for="phone">Phone</label>
<input id="phone" name="phone" type="tel" autocomplete="tel"
placeholder="555 123 4567">Common errors
- Setting
autocomplete="off"on login fields. Browsers often ignore it for passwords, and it harms users who rely on managers. - Making up tokens such as
autocomplete="zipcode". Unknown values are ignored. - Using
type="number"for identifiers. - Relying on
placeholderinstead of a label.
Exercise
A sign-up form has a field for the password the user is choosing. Which autocomplete value should it use, and why not current-password?
Show answer
Use autocomplete="new-password". It tells the password manager the user is creating a password, so it can suggest a strong one and save it. current-password signals a sign-in form, so the manager would try to fill an existing saved password.
Exercise
A field collects a five-digit postal code. Why choose type="text" with inputmode="numeric" over type="number"?
Show answer
A postal code is an identifier, not a quantity. type="number" can drop leading zeros, adds spinner controls, and accepts characters such as e. inputmode="numeric" still opens a number keyboard on phones, and pattern can enforce five digits.