HTML5 為 <form> 與 <input> 引入十餘項新屬性,大幅強化表單使用者體驗、驗證邏輯與架構彈性。
| 屬性 | 類型 | 說明 |
|---|---|---|
autocomplete | on/off | 整張表單是否啟用瀏覽器自動完成,預設 on |
novalidate | 布林 | 送出時跳過原生驗證,供自訂驗證流程使用 |
| 屬性 | 適用類型 | 說明 |
|---|---|---|
autocomplete | text, email, url, tel, password, date*, color, range | 覆蓋表單層級設定,單獨控制欄位自動完成 |
autofocus | 所有 | 頁面載入自動聚焦,每頁僅一個生效 |
form | 所有 | 指定隸屬的 <form id>,讓輸入框可在表單外部 |
formaction | submit, image | 覆蓋 form.action |
formenctype | submit, image | 覆蓋 form.enctype |
formmethod | submit, image | 覆蓋 form.method (GET/POST) |
formnovalidate | submit, image | 該按鈕送出時跳過驗證 |
formtarget | submit, image | 覆蓋 form.target (_blank, _self 等) |
height, width | image | 圖片型提交按鈕尺寸 |
list | text, search, url, tel, email, date*, number, range, color | 關聯 <datalist id> 提供建議清單 |
min, max, step | number, range, date* | 數值/日期範圍與步進限制 |
multiple | email, file | 允許多值(多檔案、多信箱以逗號分隔) |
pattern | text, search, url, tel, email, password | 正則表達式驗證(需完整匹配) |
placeholder | text, search, url, tel, email, password, number | 提示佔位文字,非 label 替代品 |
required | 除 hidden, button, submit, reset, image 外皆可 | 必填驗證 |
| 屬性 | Chrome | Firefox | Edge | Safari | IE |
|---|---|---|---|---|---|
| autocomplete | ✓ | ✓ | ✓ | ✓ | 10+ |
| autofocus | ✓ | ✓ | ✓ | ✓ | 10+ |
| form | ✓ | ✓ | ✓ | ✗ | ✗ |
| form* 覆蓋屬性 | ✓ | ✓ | ✓ | ✗ | ✗ |
| list/datalist | ✓ | ✓ | ✓ | ✗ | ✗ |
| min/max/step | ✓ | ✓ | ✓ | ✗ | ✗ |
| multiple | ✓ | ✓ | ✓ | ✓ | ✗ |
| pattern | ✓ | ✓ | ✓ | ✓ | 10+ |
| placeholder | ✓ | ✓ | ✓ | ✓ | 10+ |
| required | ✓ | ✓ | ✓ | ✓ | 10+ |
| novalidate | ✓ | ✓ | ✓ | ✓ | 10+ |
<form action="/submit" method="post" id="userForm">
<input type="email" name="email" required>
<button type="submit">一般送出</button>
<button type="submit" formaction="/admin/submit" formnovalidate>管理員強制送出(略過驗證)</button>
<button type="submit" formtarget="_blank">新分頁送出</button>
</form>
<!-- 表單外的輸入框,透過 form 屬性關聯 -->
<input type="text" name="nickname" form="userForm" placeholder="暱稱">
required + pattern:必填且格式需符合正則。novalidate(表單)或 formnovalidate(按鈕):完全跳過原生驗證,適合自訂驗證庫(如 Yup、Zod)。:valid、:invalid、:required、:optional CSS 偽類可直接美化驗證狀態。