Sub topic 11
Autocomplete and Input Hints
This chapter assumes you can build a form with labeled text inputs, know that the name attribute decides what gets submitted, and have seen the common type values such as email, tel, and number. Here you will learn how browsers fill in fields for the user, and which attributes tune the keyboard, the suggestions, and the hints shown on screen.
What autocomplete does
Browsers keep data the user has typed before: names, addresses, emails, saved passwords, and payment cards. The autocomplete attribute tells the browser what kind of data a field expects, so it can offer the right saved value. It also lets password managers recognize login and signup fields.
Without the attribute, the browser guesses from the name, id, and label text. Guessing often works, but it fails for fields called field_3 or for labels in another language. An explicit token removes the guesswork.
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email" required>Autocomplete also supports accessibility. WCAG 2.1 success criterion 1.3.5 (Identify Input Purpose) asks that fields collecting personal data about the user declare their purpose in a machine-readable way. The autocomplete token is the standard way to do that. People with motor or cognitive disabilities benefit most, because they can avoid retyping.
The on and off values
The simplest values are on and off.
autocomplete="on"lets the browser fill the field. This is the default.autocomplete="off"asks the browser not to save or suggest values.
You can also set the attribute on the <form> element. Every control inside inherits that value, unless it sets its own.
<form action="/search" method="get" autocomplete="off">
<label for="q">Search</label>
<input id="q" name="q" type="search">
</form>off is a request, not a rule. Browsers often ignore it on login fields, because password managers are valuable to users. Do not rely on off to protect sensitive data. If a value must not be stored, avoid collecting it, or handle it on the server side.
Use off for data that makes no sense to save, such as a one-time code, a CAPTCHA answer, or a search box with private queries.
Autofill tokens
For personal data, use a specific token instead of on. The token names the kind of value the field holds. The browser then matches it to a stored profile.
| Token | Data it asks for |
|---|---|
| `name` | Full name |
| `given-name` | First name |
| `family-name` | Last name |
| `email` | Email address |
| `tel` | Full telephone number |
| `username` | Account username |
| `current-password` | Existing password, for login forms |
| `new-password` | New password, for signup and change forms |
| `one-time-code` | A code sent by SMS or email |
| `street-address` | Street address, possibly several lines |
| `address-line1`, `address-line2` | Individual address lines |
| `postal-code` | ZIP or postal code |
| `country` | Country code |
| `cc-number` | Payment card number |
| `bday` | Birthday |
Match the token to what the field really collects. A wrong token fills wrong data, which is worse than no token.
Passwords
current-password and new-password matter a great deal. A password manager uses them to decide whether to offer a saved password or to generate a new one. A signup form that uses current-password on the new field confuses the manager and may fill an old password into it.
<form action="/login" method="post">
<label for="user">Username</label>
<input id="user" name="username" autocomplete="username" required>
<label for="pw">Password</label>
<input id="pw" name="password" type="password"
autocomplete="current-password" required>
<button type="submit">Log in</button>
</form>One-time codes
For a code that arrives by SMS, autocomplete="one-time-code" lets some mobile browsers offer the code from the message with one tap. Pair it with inputmode="numeric" (covered below) so the keypad is numeric.
Sections and contact types
Some forms collect two sets of the same data, such as shipping and billing addresses. A section prefix keeps the sets apart. The prefix starts with section-, followed by a name you choose, then the token.
<label for="ship-zip">Shipping ZIP code</label>
<input id="ship-zip" name="ship_zip" autocomplete="section-ship postal-code">
<label for="bill-zip">Billing ZIP code</label>
<input id="bill-zip" name="bill_zip" autocomplete="section-bill postal-code">You can also add shipping or billing before the token, as in shipping postal-code. A contact hint such as home, work, or mobile goes before tel or email tokens, as in work email. The order of the parts is: optional section, optional shipping or billing, optional contact hint, then the field token.
Hints for the keyboard: inputmode and enterkeyhint
The type attribute also changes validation and behavior, which is sometimes unwanted. A ZIP code looks numeric, but type="number" would strip leading zeros, show spinner buttons, and accept values like 1e3. Use a text input with inputmode instead.
inputmode tells touch devices which virtual keyboard to show. It does not validate anything.
| Value | Keyboard |
|---|---|
| `numeric` | Digits only |
| `decimal` | Digits and a decimal separator |
| `tel` | Telephone keypad |
| `email` | Keyboard with `@` and `.` prominent |
| `url` | Keyboard with `/` and `.com` |
| `search` | Keyboard with a search action key |
| `none` | No virtual keyboard |
enterkeyhint changes the label or icon of the Enter key. Useful values are enter, done, go, next, previous, search, and send.
<label for="zip">ZIP code</label>
<input id="zip" name="zip" type="text" inputmode="numeric"
autocomplete="postal-code" pattern="[0-9]{5}" enterkeyhint="next">Suggestions with datalist
A datalist offers a list of suggestions while the user types. Unlike a select, the user may still type any value.
Link the input to the list with the list attribute, which must match the id of the datalist.
<label for="browser">Favorite browser</label>
<input id="browser" name="browser" list="browsers">
<datalist id="browsers">
<option value="Firefox">
<option value="Chrome">
<option value="Safari">
<option value="Edge">
</datalist>The submitted value is whatever ends up in the input. The server must still check it, because a datalist does not restrict input. If you need a closed set of choices, use a select or a radio group.
Placeholder and other text hints
The placeholder attribute shows faint example text inside an empty field. It has real limits:
- It disappears when the user types, so they cannot check it later.
- Its default color often has low contrast.
- It is not a replacement for a
label. Some assistive technology ignores it as a name.
Use a placeholder only for a short format example, such as [email protected]. For anything the user must read while typing, put the text in a visible element and connect it with aria-describedby.
<label for="phone">Phone number</label>
<input id="phone" name="phone" type="tel" autocomplete="tel"
aria-describedby="phone-help">
<p id="phone-help">Include the country code, for example +49 30 123456.</p>A screen reader announces the label first and then the description. The help text stays visible after typing, unlike a placeholder.
The title attribute is another weak option. It shows as a tooltip on hover, which touch users cannot trigger. Keep it as a supplement to pattern, where browsers may include it in the error message.
Related attributes that affect input
A few other attributes shape how typing behaves.
autocapitalizecontrols automatic capitalization on virtual keyboards. Useautocapitalize="none"for usernames and emails.spellcheck="false"stops the browser from underlining valid codes or names as errors.autofocusmoves focus to a field on load. Use it sparingly, since it can skip past content for screen reader users and scroll the page unexpectedly.
Common mistakes
- Using
autocomplete="off"on login fields and expecting it to work. Browsers usually ignore it there. - Using
current-passwordon a signup field. Usenew-password. - Using
type="number"for identifiers such as ZIP codes, card numbers, or phone numbers. These are labels made of digits, not quantities. - Relying on
placeholderinstead of a label. - Assuming a
datalistrestricts input. - Giving two fields the same token without sections when the form holds two addresses.
Checking your work
Open the form in a browser and look at what it offers. Click into the email field and confirm that a saved address appears. On a phone or in the browser's device emulation, confirm that the ZIP field shows a numeric keypad. Then test with a screen reader or the accessibility panel in developer tools. The accessible description of the phone field should include the help text.
Exercise
A signup form has a ZIP code field with type="number". A user in Boston enters 02134 and the server receives 2134. Name the cause and give a replacement input that keeps the leading zero, shows a numeric keypad, and lets the browser fill the value.
Show answer
A number input treats the value as a quantity, so the leading zero is not preserved by the number semantics and the field shows spinners and accepts forms like 1e3. A ZIP code is an identifier, not a quantity. Use a text input: <input id="zip" name="zip" type="text" inputmode="numeric" autocomplete="postal-code" pattern="[0-9]{5}">. The inputmode attribute gives the numeric keypad, autocomplete="postal-code" lets the browser fill the value, and pattern validates five digits.
Exercise
A page has a shipping address and a billing address, each with a postal code field. How do you let the browser fill each one from the right saved address?
Show answer
Add section prefixes to the tokens, for example autocomplete="section-ship shipping postal-code" on the first field and autocomplete="section-bill billing postal-code" on the second. The section name groups fields so the browser treats the two sets of data separately.