Autocomplete and Input Modes

Intermediate · 14 min

How autocomplete lets browsers and password managers fill fields, how inputmode picks the on-screen keyboard, and why type=number is wrong for codes.

Before thisThe Constraint Validation API

Chrome for Developers · 14 min

Autocomplete and Input Modes

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Chrome for DevelopersReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `autocomplete` tells the browser what a field is, using a fixed vocabulary of tokens such as `email`, `name`, `street-address`, `postal-code`, `new-password`, `current-password` and `one-time-code`. With the right token, browsers and password managers can fill the field correctly.
  • Use `new-password` on sign-up and password-change fields and `current-password` on login, with `username` on the account field. This tells the password manager when to offer to save a password and when to suggest a strong one.
  • `inputmode` chooses the on-screen keyboard without changing validation: `numeric` for digits, `decimal` for decimals, `tel`, `email`, `url`, `search`. It is the right tool for codes and identifiers where `type="number"` is wrong.
  • Do not use `autocomplete="off"` to stop password managers; browsers commonly ignore it for credentials. Use it for one-off fields like a coupon, where remembered values are unhelpful.
  • Reading `input.autocomplete` in a test returned the tokens in Chromium and WebKit but an empty string in Firefox, so use `getAttribute("autocomplete")` in scripts.

Typing is the expensive part

On a phone, filling in a form means typing on a small keyboard that is often the wrong one for the field. Every field that the browser fills in for you is a field the user does not have to type, and every wrong keyboard costs a few seconds and some mistakes. These two attributes address both costs.

autocomplete says what a field means, so that the browser can fill it. inputmode says what kind of keyboard will help, so that typing is easier when the browser cannot. Neither changes what counts as valid. They are about reducing effort.

autocomplete tokens

The attribute takes values from a fixed list, defined in the HTML standard. A browser does not guess from the field’s label, so naming it is how you get the right fill. Some common tokens:

Token For
name, given-name, family-name the person’s name
email, tel contact details
username the account name on login and sign-up
new-password, current-password sign-up or password change, and login
one-time-code a code sent by SMS or email
street-address, postal-code, country address parts
cc-number, cc-exp, cc-csc payment card details

You can scope a group with a prefix: shipping postal-code and billing postal-code let the browser keep two addresses apart. A test confirmed the browser keeps a multi-token value as written: section-billing shipping street-address was returned unchanged by input.autocomplete in Chromium and WebKit. In Firefox it returned an empty string for the same input, which I did not expect. If a script needs the value, use getAttribute("autocomplete"), which returns the same thing everywhere.

I did not test the filling itself. It depends on the user’s saved data and the browser’s settings, and headless browsers have none. Treat the token list as spec, and check on a real device.

Passwords

The password tokens matter most. A login form should have autocomplete="username" on the account field and autocomplete="current-password" on the password. A sign-up or password-change form should have new-password on every new-password field. Password managers use the difference to decide when to offer a stored password, when to suggest a generated one and when to offer to save. With type="password" and no token, they have to guess.

The temptation is autocomplete="off" on login forms, to stop saved credentials from being filled. Browsers commonly ignore it on credential fields, because fighting the password manager makes users choose weaker passwords. Leave it out. off is for fields where a remembered value is wrong, such as a one-off coupon or a field that holds sensitive data you do not want the browser to keep.

Accessibility has a stake too. WCAG 1.3.5, Identify Input Purpose, expects fields that collect personal data to carry a correct autocomplete token, so assistive tools and users with cognitive disabilities can fill them reliably.

inputmode: a keyboard hint

inputmode selects which virtual keyboard the browser shows, and nothing else:

  • numeric: digits only
  • decimal: digits with a decimal separator
  • tel: a phone keypad
  • email: a keyboard with @ and . close to hand
  • url: a keyboard with / and .com
  • search: a search action key
  • none: no virtual keyboard, for fields that supply their own

It does not validate. A user with a hardware keyboard can type letters into an inputmode="numeric" field, so add pattern or check the value if it matters. In a test, input.inputMode returned numeric in Chromium, Firefox and WebKit. According to web-features 3.47.0, inputmode is Baseline widely available, in Chrome 66, Firefox 95 and Safari 12.1.

The number input pitfall, revisited

inputmode is what input types pointed to when it warned against type="number" for identifiers. A verification code field done right is:

<input name="code" type="text" inputmode="numeric"
       autocomplete="one-time-code" pattern="[0-9]{6}" required>

The user gets a number pad, an SMS code that can be suggested above the keyboard on devices that support it, and validation that says six digits. It also avoids the problems of a number input, where e is typeable and the value is empty if the text is not a number.

enterkeyhint changes the label on the Enter key (“search”, “next”, “done”). autocapitalize and spellcheck control text assistance; turn them off for usernames and emails. They are hints too, and behavior varies by device.

What the video covers

The embed is the whole of Chrome for Developers’ “Sign-up form best practices” (December 2020), a Chrome Dev Summit session by Sam Dutton. Its description says it is about helping users sign up, sign in and manage their account details with a minimum of fuss, and links to the matching web.dev article. It has no chapters, and I could not read a transcript, so I cannot list what it demonstrates. It is the pick because it comes from Chrome’s own developer-relations team and concerns the forms where autocomplete and inputmode matter most. It is nearly six years old, and the runner-up, the 2020 sign-in video, is the same age; the attribute vocabulary has not changed in a way that breaks them, but check anything about password managers against current behavior. The input-types, token and reflection details in this lesson are mine, and the reflection test ran in headless Chromium, Firefox and WebKit.

Where this fits

This follows the constraint validation API and comes before file inputs and uploads. The sign-in side is part of the larger story in authentication patterns for frontend apps.

Check yourself

  1. 01A sign-up form has an email field, a new password field and a confirm-password field. Which `autocomplete` values go on them?

    `autocomplete="email"` (or `username` if the email is the account name) on the email, and `autocomplete="new-password"` on both password fields. `new-password` tells the password manager to offer a generated password and to save it after sign-up, instead of filling an old one.

  2. 02You build a six-digit verification code field with `type="number"`. What goes wrong, and what is the better markup?

    A number input accepts `e`, `+` and `.`, may drop the formatting of leading zeros, and gives a spinner that makes no sense for a code. Use `type="text"`, `inputmode="numeric"`, `autocomplete="one-time-code"` and a `pattern` such as `[0-9]{6}`.

  3. 03What does `inputmode="numeric"` do to validation?

    Nothing. It only suggests which on-screen keyboard to show. A user on a hardware keyboard can still type letters, so add a `pattern` if you need digits only. In a test, the `inputMode` property reflected `numeric` in all three browsers.

  4. 04A designer wants `autocomplete="off"` on the login form to stop saved passwords being filled. Is that a good idea?

    No. Browsers commonly ignore `off` on credential fields because letting people use a password manager is better for security. Do not fight it: label the fields with `username` and `current-password` and let the manager do its job.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark