本文解析 HTML input autocomplete 的 WHATWG Token 語法、常用屬性值與三個標準表單範本,協助你讓瀏覽器與密碼管理器準確帶入姓名、地址、信用卡與驗證碼。
想了解
label的for屬性與輸入框的關聯,可前往 為什麼 label 的 for 屬性是 UX 與無障礙設計的關鍵;手機鍵盤(inputmode)與正則驗證(pattern)見 HTML5 input 的 inputmode 與 pattern 屬性解析與實作要點。本文直接切入autocomplete屬性。
想直接查表或複製範本,可跳到 常用屬性值對照表 或 三個標準表單範本。地址、信用卡拆欄、OTP 與 auto* 屬性分工等實際遇到的問題,見 HTML input autocomplete 常見問題。
一、從預設歷史紀錄到結構化自動填入:Autocomplete 與 Autofill 的運作差異
在配置屬性值之前,先看一般表單不加任何設定時的預設行為,以及瀏覽器為什麼常在複雜表單中填錯欄位。
1. 原本正常的標準寫法與瓶頸:未指定或僅依賴 name 屬性的問題
不寫 autocomplete 時,<input> 沿用所屬 <form> 的 autocomplete 狀態;<form> 也沒有設定(或欄位不屬於任何表單)時,就等同 autocomplete="on"。許多表單只靠欄位的 name 或 id 設計:
<!-- 傳統標準寫法:未明確指定 autocomplete 語意 Token,僅依賴自訂的 name 屬性 -->
<form action="/checkout" method="POST">
<div class="form-field">
<label for="buyer-name">購買人姓名</label>
<input type="text" id="buyer-name" name="buyer_full_name" required>
</div>
<div class="form-field">
<label for="recv-name">收件人姓名</label>
<input type="text" id="recv-name" name="receiver_full_name" required>
</div>
<div class="form-field">
<label for="recv-addr">收件地址</label>
<input type="text" id="recv-addr" name="addr_line_1" required>
</div>
</form>
只依賴預設行為時,會面臨兩個限制:
- 歷史紀錄自動完成(Autocomplete)缺乏跨站語意:
autocomplete="on"只表示允許提供建議值,瀏覽器只能依name、id等線索比對過去輸入的值,A 網站的user_phone與 B 網站的mobile_no無法互通。 - 結構化自動填入(Autofill)的啟發式猜測(Heuristics)容易誤判:瀏覽器與密碼管理器(1Password、Bitwarden、iCloud 鑰匙圈)內建了姓名、多組地址、信用卡與登入憑證的資料儲存庫,但未指定
autocomplete時,只能依label文字、name與placeholder等線索,用啟發式規則(例如比對正規表達式)猜測欄位用途。同一頁同時出現「購買人地址」與「收件人地址」,或name是無語意的field_01時,就容易填錯欄位。
2. WHATWG 規範架構:五段式 Autofill Detail Tokens 語法組合規則
為了解決猜測的不確定性,WHATWG HTML Living Standard 的 Autofill 一節 定義了 Autofill Detail Tokens 語法。on、off 只能單獨使用,其餘寫法是以空白分隔、由左至右排列的 五段式 Token 組合:
| 排列順序 | Token 類別 | 允許的規範值範例 | 核心用途說明 |
|---|---|---|---|
| 第 1 段(選填) | 區段命名(Section) | section-sender、section-recipient | 必須以 section- 開頭,將同一張表單內的多組相同欄位(如寄件人與收件人)分成獨立群組 |
| 第 2 段(選填) | 地址/交易角色 | shipping(收件)、billing(帳單) | 區分電商結帳時的「商品寄送地址」與「信用卡帳單地址」 |
| 第 3 段(選填) | 通訊媒介修飾詞 | home、work、mobile、fax、pager | 只能接在聯絡類欄位名稱前(tel、tel-*、email、impp),指定住家、公司或行動裝置聯絡方式 |
| 第 4 段(必填) | 核心欄位名稱 | name、street-address、tel、cc-number、current-password | 指定該輸入框實際對應的資料欄位型別 |
| 第 5 段(選填) | WebAuthn 憑證提示 | webauthn | 置於最末端,表示瀏覽器應列出透過 Conditional Mediation 取得的 Passkey;只適用於 <input> 與 <textarea> |

例如 autocomplete="section-primary shipping work tel" 宣告「primary 區段、用於收件(shipping)的公司(work)電話號碼(tel)」。各段 Token 的先後順序不可顛倒:寫成 tel work shipping 會被當成無效值,input.autocomplete 回傳空字串,欄位退回與未指定語意時相同的 on/off 預設行為。
二、常用 autocomplete 標準屬性值分類對照表
WHATWG 定義的數十種核心欄位名稱(第 4 段 Token),可歸納為三類:
1. 身分認證與安全憑證欄位
這類屬性影響瀏覽器內建密碼庫與第三方密碼管理器的行為:
autocomplete 屬性值 | 對應欄位與瀏覽器行為 |
|---|---|
username | 自動帶入已儲存的登入帳號(即使帳號格式為 Email 或手機號碼,在登入與註冊表單中亦建議標記為 username 以利密碼庫配對) |
current-password | 自動帶入該網域目前已儲存的登入密碼 |
new-password | 提示瀏覽器這是新密碼欄位,瀏覽器可據此避免帶入舊密碼,並協助產生高強度密碼 |
one-time-code | 標示用來驗證身分的一次性驗證碼(常見來源為簡訊、Email 或驗證器 App);Apple 文件說明 Safari 可藉此自動填入簡訊登入流程收到的驗證碼 |
2. 個人聯絡資訊與多語系姓名拆分
華語圈常用「全名單一欄位」,歐美則多為「姓氏/名字拆分欄位」:
autocomplete 屬性值 | 對應欄位與瀏覽器行為 |
|---|---|
name | 完整姓名(不分姓與名,最符合台灣與東亞表單習慣) |
family-name / given-name | 姓氏(Last Name / Surname)與名字(First Name / Given Name) |
email | 電子郵件信箱(可搭配 home email 或 work email) |
tel | 完整電話號碼(國碼與本地號碼拆開時,用 tel-country-code 與 tel-national) |
organization / organization-title | 公司或機構名稱/個人職稱 |
3. 電商地址與信用卡支付欄位
結帳流程中,正確標記地址與信用卡 Token 能降低輸入摩擦:
autocomplete 屬性值 | 對應欄位與瀏覽器行為 |
|---|---|
country / postal-code | 國家代碼(ISO 3166-1 alpha-2,如 TW)與郵遞區號(如 106 或 10608) |
address-level1 / address-level2 | 第一級行政區(台灣為「縣市」,如台北市;美國為「州 State」)與第二級行政區(台灣為「鄉鎮市區」,如大安區;美國為「城市 City」) |
street-address | 完整街道門牌地址(可多行,規範限用於 <textarea>、<select> 或隱藏欄位;單行 <input> 請改用 address-line1、address-line2) |
cc-name / cc-number | 持卡人姓名與信用卡卡號 |
cc-exp(或 cc-exp-month / cc-exp-year) | 信用卡有效期限(MM/YY 單一欄或拆分為月、年兩欄) |
cc-csc | 信用卡安全碼(CVV / CVC) |
三、三個核心表單場景的標準實作架構
以下用登入驗證、電商雙地址結帳與密碼修改三個場景,示範合乎規範的 HTML 實作。
1. 場景一:支援密碼管理器與 Passkey(通行金鑰)的登入表單
在現代登入表單中,我們可以在帳號欄同時設定 autocomplete="username webauthn"。當使用者聚焦於帳號欄時,瀏覽器除了顯示已儲存的傳統帳號密碼外,若使用者裝置存有該網站的 WebAuthn Passkey(通行金鑰),也會一併在同一個自動填入選單中列出(即 Conditional UI 機制)。要讓 Passkey 出現在選單中,頁面載入時還必須以 JavaScript 呼叫帶有 mediation: 'conditional' 選項的 navigator.credentials.get(),只寫 HTML 屬性並不會啟動這個流程:
<form id="login-form" action="/api/auth/login" method="POST">
<div class="form-field">
<label for="login-account">帳號或電子郵件</label>
<input
type="text"
id="login-account"
name="username"
autocomplete="username webauthn"
autocapitalize="none"
autocorrect="off"
spellcheck="false"
required
>
</div>
<div class="form-field">
<label for="login-password">登入密碼</label>
<input
type="password"
id="login-password"
name="password"
autocomplete="current-password"
required
>
</div>
<button type="submit">登入系統</button>
</form>
2. 場景二:修改密碼表單(同時存在舊密碼與新密碼)
在會員中心的「修改密碼」頁面中,畫面會同時出現三個 type="password" 欄位。如果沒有明確區分 current-password 與 new-password,密碼管理器很容易把舊密碼填進新密碼欄,或在儲存時把舊密碼覆蓋掉。此外,我們也可以放入一個隱藏的 username 欄位,協助密碼管理器確認此次更新密碼對應的是哪一個帳號:
<form id="change-pwd-form" action="/api/account/password" method="POST">
<!-- 隱藏的帳號欄位:協助密碼管理器精準關聯目前更新密碼的帳號名稱 -->
<input
type="text"
name="username"
value="[email protected]"
autocomplete="username"
hidden
readonly
>
<div class="form-field">
<label for="old-pwd">目前密碼</label>
<input
type="password"
id="old-pwd"
name="currentPassword"
autocomplete="current-password"
required
>
</div>
<div class="form-field">
<label for="new-pwd">設定新密碼</label>
<input
type="password"
id="new-pwd"
name="newPassword"
autocomplete="new-password"
minlength="12"
required
>
</div>
<div class="form-field">
<label for="confirm-pwd">再次確認新密碼</label>
<input
type="password"
id="confirm-pwd"
name="confirmPassword"
autocomplete="new-password"
minlength="12"
required
>
</div>
<button type="submit">更新密碼</button>
</form>
3. 場景三:電商結帳雙地址與信用卡表單(利用 shipping 與 billing 隔離)
當結帳頁同時包含「收件人地址」與「發票/帳單寄送地址」時,只要依序加上 section-* 與 shipping / billing 前綴,瀏覽器就能一次把對應的整組地址(郵遞區號、縣市、行政區、街道)填入正確區塊,不會互相覆蓋:
<form id="checkout-form" action="/api/checkout" method="POST">
<fieldset>
<legend>商品收件資訊</legend>
<div class="form-field">
<label for="ship-name">收件人姓名</label>
<input type="text" id="ship-name" name="shipName" autocomplete="section-ship shipping name" required>
</div>
<div class="form-field">
<label for="ship-tel">收件人手機</label>
<input type="tel" id="ship-tel" name="shipTel" inputmode="tel" autocomplete="section-ship shipping mobile tel" required>
</div>
<div class="form-field">
<label for="ship-zip">郵遞區號</label>
<input type="text" id="ship-zip" name="shipZip" inputmode="numeric" pattern="[0-9]{3,6}" autocomplete="section-ship shipping postal-code" required>
</div>
<div class="form-field">
<label for="ship-city">縣市</label>
<input type="text" id="ship-city" name="shipCity" autocomplete="section-ship shipping address-level1" required>
</div>
<div class="form-field">
<label for="ship-district">鄉鎮市區</label>
<input type="text" id="ship-district" name="shipDistrict" autocomplete="section-ship shipping address-level2" required>
</div>
<div class="form-field">
<label for="ship-street">街道門牌地址</label>
<textarea id="ship-street" name="shipStreet" rows="2" autocomplete="section-ship shipping street-address" required></textarea>
</div>
</fieldset>
<fieldset>
<legend>帳單寄送地址</legend>
<div class="form-field">
<label for="bill-zip">郵遞區號</label>
<input type="text" id="bill-zip" name="billZip" inputmode="numeric" pattern="[0-9]{3,6}" autocomplete="section-bill billing postal-code" required>
</div>
<div class="form-field">
<label for="bill-city">縣市</label>
<input type="text" id="bill-city" name="billCity" autocomplete="section-bill billing address-level1" required>
</div>
<div class="form-field">
<label for="bill-district">鄉鎮市區</label>
<input type="text" id="bill-district" name="billDistrict" autocomplete="section-bill billing address-level2" required>
</div>
<div class="form-field">
<label for="bill-street">街道門牌地址</label>
<textarea id="bill-street" name="billStreet" rows="2" autocomplete="section-bill billing street-address" required></textarea>
</div>
</fieldset>
<fieldset>
<legend>信用卡付款資訊</legend>
<div class="form-field">
<label for="card-name">持卡人姓名</label>
<input type="text" id="card-name" name="ccName" autocomplete="cc-name" required>
</div>
<div class="form-field">
<label for="card-number">信用卡卡號</label>
<input type="text" id="card-number" name="ccNumber" inputmode="numeric" pattern="[0-9\s]{13,19}" autocomplete="cc-number" required>
</div>
<div class="form-field">
<label for="card-exp">有效期限(MM/YY)</label>
<input type="text" id="card-exp" name="ccExp" inputmode="numeric" placeholder="MM/YY" autocomplete="cc-exp" required>
</div>
<div class="form-field">
<label for="card-csc">背面末三碼(CVC)</label>
<input type="text" id="card-csc" name="ccCsc" inputmode="numeric" pattern="[0-9]{3,4}" maxlength="4" autocomplete="cc-csc" required>
</div>
</fieldset>
<button type="submit">確認付款</button>
</form>
四、瀏覽器安全邊界與無障礙合規(WCAG 2.2 SC 1.3.5)
除了提升填表效率,autocomplete 也與密碼管理器行為及 W3C 無障礙標準有關。
1. 瀏覽器何時才提供自動填入?autocomplete="off" 能擋住什麼?
WHATWG 規範只定義 autocomplete 各個值的語意,何時跳出建議、帶入哪一筆資料,由各瀏覽器自行決定。實作時有兩點最常被誤解:
- 欄位與表單結構的前提:MDN 的 autocomplete 說明指出,瀏覽器可能要求欄位具備
name或id、位於<form>之內,且表單有送出按鈕。 autocomplete="off"不等於擋住密碼管理器:MDN 註明,在多數現代瀏覽器中,off並不會阻止密碼管理器詢問是否儲存帳號密碼,也不會阻止它在登入表單自動帶入。off適合用在驗證碼、CAPTCHA 這類一次性欄位,不適合整張表單一律關閉;後台表單避免誤填的做法,見 HTML input 自動填入(Autofill)常見問題解析:樣式覆蓋、關閉策略與狀態同步。
2. WCAG 2.2 SC 1.3.5(識別輸入目的)
根據 W3C WCAG 2.2 成功準則 1.3.5 Identify Input Purpose(Level AA),收集「使用者本人」資訊的欄位,只要用途列在 WCAG 的「Input Purposes for User Interface Components」清單中,且所用技術支援標示輸入用途(HTML 的 autocomplete 即是),就必須能以程式化方式判讀輸入目的:
- 降低記憶與操作負擔:自動填入能免除反覆查閱證件與逐字敲擊鍵盤。
- 提供熟悉的圖示:WCAG 說明文件指出,輔助科技可以依欄位用途顯示使用者熟悉的圖示(例如
autocomplete="bday"的生日欄位旁顯示生日蛋糕圖示)。
五、總結
- 遵循五段式 Token 順序:順序顛倒會被當成無效值。
- 依場景選對值:登入用
username、current-password,新密碼用new-password,簡訊驗證碼用one-time-code;地址與信用卡欄位對照第二章的表。 - 別把
off當萬用開關:它擋不住登入欄位的密碼管理器。
欄位被填錯時,用 chrome://autofill-internals 檢視判讀的做法見 HTML input autocomplete 常見問題;autocomplete="off" 被忽略、底色覆蓋與框架狀態同步見 HTML input 自動填入(Autofill)常見問題解析。
參考資料與延伸閱讀
- WHATWG HTML Living Standard — Autofill
- MDN Web Docs — HTML attribute: autocomplete
- W3C WCAG 2.2 — Understanding SC 1.3.5: Identify Input Purpose (Level AA)
- web.dev — Learn Forms: Autofill
- web.dev — Sign in with a passkey through form autofill (Conditional UI)
- Apple Developer — Enabling Password AutoFill on an HTML input element
- 前端 Input 語法教學:inputmode 鍵盤對應、CSS 跑版除錯與常見錯誤解決方法
發佈留言