HTML5 引入 13 種新的 ``,提供原生 UI、驗證、鍵盤優化與語意化,大幅減少 JavaScript 驗證與自定義控件開發成本。
| Type | 用途 | 原生 UI/鍵盤 | 驗證規則 | 屬性範例 |
|---|---|---|---|---|
email | 電子郵件 | 鍵盤含 @ . _ | 符合 RFC 5322 簡化格式 | multiple (多信箱) |
tel | 電話號碼 | 數字鍵盤 (含 * #) | **無內建格式驗證** (全球格式差異大) | pattern="[0-9+\-() ]{7,}" |
url | 網址 | 鍵盤含 : / . | 必含 scheme (https://...) | |
number | 數值 | 數字鍵盤 + 微調按鈕 | 數值型別、min/max/step | min="0" max="100" step="0.1" |
range | 範圍滑桿 | 滑桿 UI | 數值、min/max/step | min="0" max="100" step="1" |
date | 日期 (年月日) | 日期選擇器 | ISO 8601 (YYYY-MM-DD) | min="2024-01-01" max="2024-12-31" |
datetime-local | 本地日期時間 (無時區) | 日期時間選擇器 | YYYY-MM-DDTHH:MM | step="60" (分鐘間隔) |
month | 年月 | 年月選擇器 | YYYY-MM | |
week | 年週 | 週選擇器 | YYYY-Www | |
time | 時間 (時分秒) | 時間選擇器 | HH:MM[:ss[.mmm]] | step="300" (5分鐘間隔) |
color | 顏色選擇器 | 調色盤 UI | #RRGGBB (7 字元) | |
search | 搜尋框 | 帶清除按鈕、圓角 (WebKit) | 無特殊驗證 | placeholder、autosave、results |
file | 檔案選擇 | 檔案選擇對話框 | accept、multiple、capture | accept="image/*,.pdf" |
required:必填placeholder:提示文字min/max/step:數值/日期/時間範圍與步進pattern:正則表達式驗證 (適用 text, tel, url, email, password 等)autocomplete:自動填充提示 (on/off/email/username/current-password 等)list:關聯 `const input = document.querySelector('input[type="email"][required]');
// 檢查有效性
if (!input.validity.valid) {
console.log(input.validationMessage); // 瀏覽器內建錯誤訊息 (已本地化)
input.reportValidity(); // 顯示氣泡提示並聚焦
}
// 自訂錯誤訊息
input.setCustomValidity('請輸入公司信箱 (結尾 @company.com)');
if (!input.value.endsWith('@company.com')) {
input.setCustomValidity('請輸入公司信箱 (結尾 @company.com)');
} else {
input.setCustomValidity(''); // 清除自訂錯誤
}
input:valid { border-color: #28a745; }
input:invalid { border-color: #dc3545; }
input:required:invalid { background: #fff0f0; }
input:focus:invalid { box-shadow: 0 0 0 0.2rem rgba(220,53,69,.25); }
input:in-range { background: #f0fff0; }
input:out-of-range { background: #fff0f0; }
| Type | Chrome | Firefox | Safari | Edge | 回退行為 |
|---|---|---|---|---|---|
| email, url, tel, number | ✅ | ✅ | ✅ | ✅ | 退化為 text |
| date, datetime-local, month, week, time | ✅ | ✅ (部分 UI 差異) | ✅ (iOS 14+ / macOS 11+) | ✅ | 退化為 text,**需 Polyfill** (如 better-dateinput-polyfill) |
| color | ✅ | ✅ | ✅ | ✅ | 退化為 text (需 7 字元 #RRGGBB) |
| range | ✅ | ✅ | ✅ | ✅ | 退化為 text (需 JS 滑桿 Polyfill) |
| search | ✅ | ✅ | ✅ | ✅ | 退化為 text |
<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="UTF-8">
<title>HTML5 Input Types Demo</title>
<style>
form { max-width: 400px; margin: 2rem auto; font-family: system-ui; }
label { display: block; margin: 1rem 0 0.25rem; font-weight: 500; }
input { width: 100%; padding: 0.5rem; font-size: 1rem; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; }
input:invalid { border-color: #dc3545; }
input:valid { border-color: #28a745; }
button { margin-top: 1rem; padding: 0.75rem 1.5rem; background: #0d6efd; color: white; border: none; border-radius: 4px; cursor: pointer; }
</style>
</head>
<body>
<form id="demo-form" novalidate>
<label>Email: <input type="email" name="email" required placeholder="user@example.com"></label>
<label>手機: <input type="tel" name="phone" pattern="09\d{8}" required placeholder="0912345678" title="09 開頭 10 碼"></label>
<label>網址: <input type="url" name="url" placeholder="https://example.com"></label>
<label>年齡 (18-100): <input type="number" name="age" min="18" max="100" step="1"></label>
<label>滿意度 (1-10): <input type="range" name="satisfaction" min="1" max="10" value="5"></label>
<label>生日: <input type="date" name="birthday" max="2006-12-31"></label>
<label>約會時間: <input type="datetime-local" name="appt"></label>
<label>主題色: <input type="color" name="theme" value="#0d6efd"></label>
<label>搜尋: <input type="search" name="q" placeholder="關鍵字" autosave="mysearch" results="5"></label>
<button type="submit">送出 (開啟主控台查看 validity)</button>
</form>
<script>
document.getElementById('demo-form').addEventListener('submit', e => {
e.preventDefault();
const form = e.target;
if (form.checkValidity()) {
console.log('✅ 表單驗證通過', Object.fromEntries(new FormData(form)));
} else {
console.log('❌ 驗證失敗', form.reportValidity());
}
});
</script>
</body>
</html>
HTML5 新型 Input 類型是現代表單開發的基石。善用它們能獲得**原生驗證、行動裝置鍵盤優化、無障礙支援、語意化 SEO 優勢**,並大幅減少自定義 JavaScript 驗證代碼。關鍵在於理解各類型的**原生 UI 差異、驗證規則、屬性組合**,並制定**優雅降級策略**應對舊瀏覽器。