Sub topic 8
Input types and autocomplete
This chapter assumes you know basic HTML form elements and how labels attach to inputs. The goal is to cut typing effort and mistakes by telling the browser what each field holds.
Why the type attribute matters
The type attribute on an input does three jobs. It picks the on-screen keyboard on phones, it enables built-in checks such as email format, and it exposes the field's purpose to assistive technology. A plain type="text" field does none of these.
| Data | Type | What you get |
|---|---|---|
| Email address | `email` | `@` key on the mobile keyboard, format check |
| Phone number | `tel` | Number pad, no format check |
| Web address | `url` | `/` and `.com` keys, format check |
| Date | `date` | Native date picker |
| Quantity | `number` | Numeric keyboard, spinner, min and max checks |
| Search term | `search` | Clear button in some browsers |
Use tel for phone numbers. Phone numbers have spaces, plus signs, and leading zeros, and a number field can strip or reject them.
Choosing number carefully
type="number" suits quantities you could add or subtract. It does not suit identifiers such as postcodes, card numbers, or account numbers. Browsers may drop leading zeros, show spinners that change the value on scroll, and reject spaces. For digit-only identifiers, use a text field with inputmode:
<label for="code">Verification code</label>
<input id="code" name="code" type="text"
inputmode="numeric" autocomplete="one-time-code"
pattern="[0-9]{6}">inputmode="numeric" shows a number pad while keeping the value as text. The pattern attribute adds a check that the value is six digits.
Autocomplete tells the browser what to fill
The autocomplete attribute names the purpose of a field using a fixed vocabulary. The browser can then fill it from saved data, and password managers can recognize it. This also meets WCAG 1.3.5 (Identify Input Purpose) for fields that collect personal data.
<form>
<label for="name">Full name</label>
<input id="name" name="name" type="text" autocomplete="name">
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email">
<label for="tel">Phone number</label>
<input id="tel" name="tel" type="tel" autocomplete="tel">
<label for="street">Street address</label>
<input id="street" name="street" type="text" autocomplete="street-address">
<label for="postcode">Postcode</label>
<input id="postcode" name="postcode" type="text" autocomplete="postal-code">
<button type="submit">Save details</button>
</form>Common values include given-name, family-name, username, current-password, new-password, cc-number, and bday. For a sign-up form, use new-password so the manager offers to generate one. For a login form, use current-password so it fills the saved one.
Common mistakes
- Using
type="text"for everything, which loses keyboards and checks. - Using
type="number"for postcodes, phone numbers, or card numbers. - Setting
autocomplete="off"on ordinary fields. Browsers often ignore it for passwords, and it blocks help for users who struggle to type. - Using a wrong or invented autocomplete value. Browsers skip values they do not recognize.
- Relying on
typealone for validation. Browser checks are loose:emailacceptsa@b. Check on the server too.
Practice
Exercise
A checkout form asks for a UK postcode and a six-digit SMS code. Which type, inputmode, and autocomplete values would you give each field, and why not use type="number"?
Show answer
Postcode: type="text", autocomplete="postal-code". The value has letters and a space, so a numeric type would reject it. SMS code: type="text", inputmode="numeric", autocomplete="one-time-code", optionally with pattern="[0-9]{6}". This shows a number pad and allows autofill from the SMS, while keeping the value as text. type="number" can drop leading zeros, add spinners, and reject spaces, which is wrong for identifiers.
Exercise
A sign-up form's password field uses autocomplete="off". What should you change, and what does the change allow?
Show answer
Use autocomplete="new-password" on the sign-up password field. The password manager can then offer to generate and save a strong password. A login form would use current-password instead so the manager fills the saved one.