過去做表單驗證得靠一堆 JS 判斷,現在 HTML5 直接把「檢查」這件事交給瀏覽器。搭配 Constraint Validation API 的 validity 物件,你只需要負責「怎麼把錯誤說得清楚」,驗證本身不用自己寫。
這些型別不只是語意加分,在手機上還會叫出對應的鍵盤,這是實際使用者最有感的部分。
<form> <!--email 屬性--> 郵箱類型<input type="email"/><br/> <!--tel 屬性--> 電話類型<input type="tel"/><br/> <!--number 屬性--> 數字類型<input type="number"/><br/> <!--date 屬性--> 日期類型<input type="date"/><br/> <!--month 屬性--> 月份類型<input type="month"/><br/> <!--week 屬性--> 週次類型<input type="week"/><br/> <!--range 屬性--> 數字範圍<input type="range" min="18" max="60"/><br/> <!--search 屬性--> 搜尋類型<input type="search"/><br/> <!--color 屬性--> 顏色選擇器<input type="color"/><br/> <!--url 屬性--> 網址類型<input type="url"/><br/> <input type='submit'/> </form>
三個屬性要記牢:required 必填、pattern 正則約束、autofocus 自動聚焦。每個欄位後面都放一個 <p class="pc"> 當提示文字,JS 會依驗證結果切換它的樣式與內容。
<div id="form_content">
<form action="">
<div class="dc"><h3>用戶注冊頁面</h3></div>
<div class="dc"><b>用戶昵稱</b><input id="user" type="text" autofocus required pattern="^[0-9a-zA-Z]{6,10}$"/><p class="pc">請輸入用戶名</p></div>
<div class="dc"><b>用戶密碼</b><input id="pwd" type="password" required pattern="^\w{8,12}$"/><p class="pc">請輸入密碼</p></div>
<div class="dc"><b>個人郵箱</b><input id="email" type="email" required/><p class="pc">請輸入郵箱</p></div>
<div class="dc"><b>個人主頁</b><input id="url" type="url"/><p class="pc">請輸入個人主頁(可不填)</p></div>
<div class="dc"><b>聯系電話</b><input id="tel" type="tel" required/><p class="pc">請輸入聯系電話</p></div>
<div class="dc"><b>你的年齡</b><input id="age" type="number" min="18" max="60" required/><p class="pc">請輸入你的年齡</p></div>
<div class="dc"><b>出生日期</b><input id="date" type="date" required/><p class="pc">請選擇出生日期</p></div>
<div class="dc"><input id="sub" type="submit" value="提交注冊"/></div>
</form>
</div>
每個欄位的 onfocus 顯示格式說明,onblur 則依 validity 的具體欄位給出精準訊息——是「沒填」還是「格式錯」,講清楚才有用。nextElementSibling 讓你不必再寫一堆 getElementById。
var uname = document.getElementById('user');
uname.onfocus = function(){
this.nextElementSibling.style.display = 'block';
this.nextElementSibling.innerHTML = '6-10 位數字或字母組成';
}
uname.onblur = function(){
if (this.validity.valid) {
this.nextElementSibling.className = 'pc show_pass';
this.nextElementSibling.innerHTML = '用戶名格式正確';
} else if (this.validity.valueMissing) {
this.nextElementSibling.className = 'pc show_warn';
this.nextElementSibling.innerHTML = '用戶名不能為空';
} else if (this.validity.patternMismatch) {
this.nextElementSibling.className = 'pc show_warn';
this.nextElementSibling.innerHTML = '用戶名格式非法';
}
}
var upwd = document.getElementById('pwd');
upwd.onfocus = function(){
this.nextElementSibling.style.display = 'block';
this.nextElementSibling.innerHTML = '8-12 位數字/字母/英文符號組成';
}
upwd.onblur = function(){
if (this.validity.valid) {
this.nextElementSibling.className = 'pc show_pass';
this.nextElementSibling.innerHTML = '密碼格式正確';
} else if (this.validity.valueMissing) {
this.nextElementSibling.className = 'pc show_warn';
this.nextElementSibling.innerHTML = '用戶密碼不能為空';
} else if (this.validity.patternMismatch) {
this.nextElementSibling.className = 'pc show_warn';
this.nextElementSibling.innerHTML = '密碼格式非法';
}
}
var e_mail = document.getElementById('email');
e_mail.onfocus = function(){
this.nextElementSibling.style.display = 'block';
this.nextElementSibling.innerHTML = '請輸入你的常用郵箱';
}
e_mail.onblur = function(){
if (this.validity.valid) {
this.nextElementSibling.className = 'pc show_pass';
this.nextElementSibling.innerHTML = '郵箱格式正確';
} else if (this.validity.valueMissing) {
this.nextElementSibling.className = 'pc show_warn';
this.nextElementSibling.innerHTML = '郵箱不能為空';
} else if (this.validity.typeMismatch) {
this.nextElementSibling.className = 'pc show_warn';
this.nextElementSibling.innerHTML = '郵箱格式有誤';
}
}
var url = document.getElementById('url');
url.onfocus = function(){
this.nextElementSibling.style.display = 'block';
this.nextElementSibling.innerHTML = '請輸入你的個人主頁(選填)';
}
url.onblur = function(){
if (this.validity.valid) {
this.nextElementSibling.className = 'pc show_pass';
this.nextElementSibling.innerHTML = '網址格式正確';
} else if (this.validity.typeMismatch) {
this.nextElementSibling.className = 'pc show_warn';
this.nextElementSibling.innerHTML = '網址格式非法';
}
}
var uphone = document.getElementById('tel');
uphone.onfocus = function(){
this.nextElementSibling.style.display = 'block';
this.nextElementSibling.innerHTML = '請輸入你的聯系電話';
}
uphone.onblur = function(){
if (this.validity.valid) {
this.nextElementSibling.className = 'pc show_pass';
this.nextElementSibling.innerHTML = '電話號碼格式正確';
} else if (this.validity.valueMissing) {
this.nextElementSibling.className = 'pc show_warn';
this.nextElementSibling.innerHTML = '電話號碼不能為空';
} else if (this.validity.typeMismatch) {
this.nextElementSibling.className = 'pc show_warn';
this.nextElementSibling.innerHTML = '電話號碼格式非法';
}
}
var uage = document.getElementById('age');
uage.onfocus = function(){
this.nextElementSibling.style.display = 'block';
this.nextElementSibling.innerHTML = '請輸入你的年齡';
}
uage.onblur = function(){
if (this.validity.valid) {
this.nextElementSibling.className = 'pc show_pass';
this.nextElementSibling.innerHTML = '你的年齡符合注冊要求';
} else if (this.validity.rangeOverflow) {
this.nextElementSibling.className = 'pc show_warn';
this.nextElementSibling.innerHTML = '你的年齡大於注冊范圍';
} else if (this.validity.rangeUnderflow) {
this.nextElementSibling.className = 'pc show_warn';
this.nextElementSibling.innerHTML = '你的年齡小於注冊范圍';
} else if (this.validity.valueMissing) {
this.nextElementSibling.className = 'pc show_warn';
this.nextElementSibling.innerHTML = '年齡不能為空';
}
}
var udate = document.getElementById('date');
udate.onfocus = function(){
this.nextElementSibling.style.display = 'block';
this.nextElementSibling.innerHTML = '請選擇你的出生日期';
}
udate.onblur = function(){
if (this.validity.valueMissing) {
this.nextElementSibling.className = 'pc show_warn';
this.nextElementSibling.innerHTML = '出生日期不能為空';
} else if (this.validity.valid) {
this.nextElementSibling.className = 'pc show_pass';
this.nextElementSibling.innerHTML = '已選擇出生日期';
}
}
第一,前端驗證只是使用者體驗,永遠不能取代後端驗證——任何欄位都可能被繞過。第二,type="tel" 瀏覽器不會幫你檢查號碼格式,需要自己用 pattern 補上。第三,把這八段重複的 blur 邏輯抽成一個函式、用 data-* 屬性帶入提示文字,程式碼會短掉一大半,這也是這個範例最值得再優化的一處。