HTML Form Autocomplete, Autofill, and Accessible Validation
How to mark up HTML forms for browser autofill, mobile keyboards, labels, and client-side validation that does not break accessibility.
This page brings together the WHATWG HTML living standard on forms and autofill-related attributes with Chrome Modern Web Guidance and related Autofill documentation. Together they describe how to build forms that browsers can fill automatically, that work well on mobile, and that validate input without confusing users or assistive technologies.
HTML forms: semantic markup and server communication
Section titled “HTML forms: semantic markup and server communication”An HTML form is a set of form controls—text fields, buttons, checkboxes, and similar controls—that collects data a user agent can send to a server. The WHATWG introduction to forms states that no client-side scripting is required in many cases, though scripts can extend the experience. Building a form involves the UI, server processing, and wiring the UI to the server; those steps can be done in any order.
The Forms guide recommends wrapping interactive controls in a real <form>, using method="POST" for sensitive or mutating requests, setting action to the submission URL, giving every control a name, and using semantic elements such as <button type="submit">, <fieldset>, and <legend>. Prefer <button> for actions—not a <div> acting as a button—and avoid GET for sensitive data, which exposes values in history and logs.
autocomplete attribute: how browsers know what to fill
Section titled “autocomplete attribute: how browsers know what to fill”Browsers try to avoid making users retype the same personal data on every site. The WHATWG Enabling client-side automatic filling of form controls section explains that the autocomplete attribute describes a field’s purpose so the user agent can offer appropriate stored values—for example a phone field annotated for shipping contact details.
Some browsers attempt to aid the user by automatically filling form controls rather than having the user reenter their information each time. For example, a field asking for the user’s telephone number can be automatically filled with the user’s phone number.
To help the user agent with this, the autocomplete attribute can be used to describe the field’s purpose.
— WHATWG, 4.10 Forms
Chrome Autofill documentation adds that understanding autofill and correct autocomplete values saves users time, reduces mistakes, and can improve completion rates on checkout and other flows. For any <input>, <textarea>, or <select> that collects personal information, apply a correct token (for example email, given-name, or payment tokens such as cc-number). The Accessibility Coding Guidelines treat this as mandatory for WCAG 1.3.5 Identify Input Purpose (Level AA).
Stable names, real forms, and password-manager tokens
Section titled “Stable names, real forms, and password-manager tokens”For modern browsers to save and refill data, fields need stable name or id values (not regenerated on each load), and inputs must sit inside a <form> with a submit button, as described in the sign-in form guide and sign-up form guide.
| Situation | Recommended autocomplete |
Notes |
|---|---|---|
| Sign-in email | username |
Use with type="email"; username is what password managers expect. |
| Sign-in password | current-password |
Required on sign-in forms per Chrome guidance. |
| New account or reset password | new-password |
Prevents managers from filling an old password on registration. |
| Password change (new + confirm) | new-password on both new fields |
See Automated Password Change. |
| Card number | cc-number |
Use type="text" with inputmode="numeric", not type="number". |
| Shipping vs billing | Prefix tokens, e.g. shipping address-line1 |
Token is address-line1 (no hyphen before the digit). |
For sign-in, Chrome’s guide gives a minimal pattern:
<input type="email" id="email" name="email" autocomplete="username" required><input type="password" id="password" name="password" autocomplete="current-password" required>Allow pasting into password fields; blocking paste frustrates users and works against password managers, as Lighthouse best practices and the sign-in guides explain.
inputmode and type: mobile keyboards without wrong controls
Section titled “inputmode and type: mobile keyboards without wrong controls”On devices with virtual keyboards, users benefit from the right modality. The WHATWG section Improving the user experience on mobile devices shows inputmode="numeric" for fields like a buzzer code while keeping appropriate type values for email and telephone.
Some devices, in particular those with virtual keyboards can provide the user with multiple input modalities. For example, when typing in a credit card number the user may wish to only see keys for digits 0-9, while when typing in their name they may wish to see a form field that by default capitalizes each word.
Using the inputmode attribute we can select appropriate input modalities:
— WHATWG, 4.10 Forms
The Forms guide and payment form guide reinforce: use type="email" and type="tel" for email and phone; use inputmode="numeric" for PINs, card security codes, and similar numeric entry; do not use type="number" for card numbers, postal codes, or one-time codes (spinners and leading-zero issues). Pair enterkeyhint (next, done, previous) on multi-step flows so the mobile Enter key matches the step.
type, inputmode, and autocomplete control different layers—validation hint, keyboard layout, and autofill—and work best together, per the HTML guide.
Labels, hints, and accessible structure
Section titled “Labels, hints, and accessible structure”Labels are not optional decoration. Accessibility Coding Guidelines require a label for every control, programmatic association via <label for="id"> and matching id, and warn that placeholders are not persistent labels—use them only as brief hints. Link help text with aria-describedby, and place hints above the input when autofill popovers or mobile keyboards would cover text below, as stressed in the payment form guide.
Use required (or aria-required="true" only when required does not apply) for mandatory fields. Do not auto-submit or change context on focus alone (WCAG 3.2.1 / 3.2.2). For errors, do not communicate state with color alone—add text or icons, as in the color guide and Required Field Feedback.
Autofill patterns by form type
Section titled “Autofill patterns by form type”Autofill is progressive enhancement: without it, users still type manually while type, pattern, and required keep helping, as each specialized guide notes in its fallback section.
Address and name fields
Section titled “Address and name fields”The address form guide recommends a single name field when possible, Unicode-friendly validation (not Latin-only regex), and either one <textarea autocomplete="street-address"> or separate lines with address-line1, address-line2, and related tokens. Use address-level2 for city and postal-code for ZIP or postal codes.
Payment checkout
Section titled “Payment checkout”The payment form guide treats payment forms as critical to conversion: meaningful HTML, descriptive submit labels (for example “Proceed to Payment”), single-field card and phone numbers, visible format hints linked with aria-describedby, and type="text" for card security codes (autocomplete="cc-csc") so users can spot typos—avoid masking the CSC with type="password". Default billing to match shipping when possible and prefix billing vs shipping on duplicate field names.
Sign-in and sign-up
Section titled “Sign-in and sign-up”Put sign-in and sign-up each in its own <form> with a submit button—do not rely on a <div> and script-only submission, per the dedicated autofill guides. Avoid asking users to enter email or password twice; confirmation belongs in email verification or password reset flows. Add spellcheck="false" (and turn off autocorrect on non-email usernames) so identifiers are not “corrected” into the wrong string.
Optional: highlight autofilled-but-unedited fields with the CSS pseudo-class :autofill (not the incorrect :auto-fill), using more than border color alone and preserving focus outlines—see Highlight browser-autofilled fields.
Client-side validation: UX only, not security
Section titled “Client-side validation: UX only, not security”HTML constraint validation (required, pattern, type="email", and related attributes) improves feedback before submit. The WHATWG Security section is explicit about limits:
Servers should not rely on client-side validation. Client-side validation can be intentionally bypassed by hostile users, and unintentionally bypassed by users of older user agents or automated tools that do not implement these features. The constraint validation features are only intended to improve the user experience, not to provide any kind of security mechanism.
— WHATWG, Form control infrastructure
The Forms guide matches that split: client validation is for UX; server validation is for integrity. Keep submit buttons enabled and let the browser focus the first invalid control on submit rather than disabling the button to block validation.
:user-invalid and :user-valid: validate after interaction
Section titled “:user-invalid and :user-valid: validate after interaction”Styling :invalid alone can mark empty required fields “invalid” as soon as the user tabs in—before they have typed anything. Accessible Error Announcement notes that standard HTML5 validation does not automatically sync aria-invalid with visual pseudo-classes, which can produce disruptive screen reader announcements.
The fix recommended across Required Field Feedback, Validate Input After Interaction, and related guides:
- Use native constraints in HTML (
required,type="email",pattern, and so on). - Style errors with
:user-invalid(and optionally success with:user-valid) so errors appear after blur, partial edit, or submit attempt—not on first paint. - Bridge to assistive tech with JavaScript that sets
aria-invalidwhen:user-invalidmatches, and link messages viaaria-errormessageoraria-describedby.
The Forms guide summarizes timing: on input, clear errors only; on blur, show field-level errors; on submit, block and move focus. When :user-invalid is unsupported, the guides suggest feature detection with CSS.supports('selector(:user-invalid)') and a small interaction tracker before applying fallback classes.
Common mistakes
Section titled “Common mistakes”- Missing or wrong
autocompletetokens, so DevTools shows poor field mapping—inspect with the Autofill panel in DevTools. - Placeholders used instead of
<label>elements. - Splitting card or phone numbers across multiple inputs, which breaks autofill.
type="number"for payment or postal fields.autocomplete="off"on login or checkout fields.- Relying on
:invalidor color-only error states without:user-invalidand ARIA sync. preventDefault()on paste events for passwords.- Treating browser validation as a security boundary.
How to check this on your site
Section titled “How to check this on your site”-
Open your form in Chrome, enable address autofill if needed, and use the Autofill panel in DevTools to confirm each field’s detected role, predicted token, and filled value. Use the Issues panel for
autocompleteproblems mentioned in that doc. -
Tab through required fields on a fresh load: empty fields should not show error styling or
aria-invalid="true"until after interaction or submit, matching :user-invalid behavior. -
Test sign-in and sign-up with saved credentials: email should use
autocomplete="username", passwordcurrent-passwordornew-passwordas in the sign-in and sign-up guides. -
On a phone or emulator, confirm
typeandinputmodeshow the expected keyboard for email, tel, and numeric fields per WHATWG mobile guidance. -
You can check automatically in Answerwell (our tool): https://answerwell.org/workspace
Report an error or ask a question
Section titled “Report an error or ask a question”If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact wording you are asking about so it can be corrected or explained.