新type屬性介紹
-
首先讓我們來看一張表
HTML5中的type.png
其中標(biāo)有`紅色5`的代表`HTML5`中推出的
-
測試代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> form { width: 80%; background-color: #F7F7F7; } label { display: block; width: 80%; margin: 0 auto; font-size: 30px; font-weight: bold; } input { display: block; width: 80%; margin: 0 auto; } </style> </head> <body> <form action=""> <fieldset> <legend>測試type屬性 </legend> <label for="">color: </label> <input type="color"> <label for="">date: </label> <input type="date"> <label for="">datetime: </label> <input type="datetime"> <label for="">datetime-local: </label> <input type="datetime-local"> <label for="">month: </label> <input type="month"> <label for="">week: </label> <input type="week"> <label for="">time: </label> <input type="time"> <label for="">email: </label> <input type="email"> <label for="">number: </label> <input type="number"> <label for="">range: </label> <input type="range"> <label for="">search: </label> <input type="search"> <label for="">tel: </label> <input type="tel"> <input type="submit"> </fieldset> </form> </body> </html>
-
運行效果
input新type屬性.png
新type屬性的注意要點
* 點擊不同type的input標(biāo)簽會有不一樣的彈出內(nèi)容 * 如果發(fā)現(xiàn)w3cschool內(nèi)容不全,建議去MDN搜索 * 并不是每一個新type屬性,在PC端都有不同的顯示 * color, date, number 這些效果較為明顯
-
兼容性問題
-
由于ie的兼容性的問題,在不同的瀏覽器中顯示效果不盡相同
-
但是在移動設(shè)備上的支持效果較好,可以將該頁面發(fā)送到手機進(jìn)行測試
-
實際開發(fā)中可以按照需求選用
input表單驗證
用戶在輸入內(nèi)容的時候不可能做到全部正確,比如
email地址``電話長度
等等都有可能出現(xiàn)輸入錯誤,試想一下,當(dāng)用戶辛辛苦苦的輸入了10多個表單內(nèi)容,點擊提交由于輸入錯誤,內(nèi)容被清空了
w3cSchool 查閱位置
下面把api文檔的查閱位置添加如下
-
[w3cSchool_事件屬性]w3School
-
[w3cSchool_input標(biāo)簽]w3cSchool
email標(biāo)簽
在
H5
中的input
的新type
屬性
-
示例代碼:
-
當(dāng)我們點擊
提交按鈕
時,如果輸入的email
格式不正確,會彈出提示信息 -
email
標(biāo)簽并不會驗證內(nèi)容是否為空,這個需要注意
email自帶提示.png
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action=""> email:<input type="email" name="userEmail"> <br/> <input type="submit"> </form> </body> </html>
required屬性
對于沒有自帶驗證效果的標(biāo)簽,就需要手動添加屬性增加驗證了
-
使用方法:
-
只需要添加
required
屬性即可,不需要賦值 -
示例代碼:
-
當(dāng)控件沒有輸入任何內(nèi)容直接點擊提交時,會彈出提示
required屬性.png
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action=""> email:<input type="email" name="userEmail"> <br/> tel:<input type="tel" required> <br/> <input type="submit"> </form> </body> </html>
pattern 自定義驗證規(guī)則
使用
required
標(biāo)簽只能夠驗證內(nèi)容是否為空,如果想要驗證的更為準(zhǔn)確就需要自定義驗證規(guī)則了
-
使用方法:
-
在需要添加自定義驗證規(guī)則的元素中添加
required
標(biāo)簽 -
使用正則表達(dá)式編寫驗證規(guī)則
-
示例代碼:
-
當(dāng)我們輸入的內(nèi)容跟驗證條件不符時,就會彈出對應(yīng)的提示
自定義驗證.png
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action=""> email:<input type="email" name="userEmail"> <br/> tel:<input type="tel" required pattern="[0-9]{3}"> <br/> <input type="submit"> </form> </body> </html>
自定義驗證信息
系統(tǒng)的提示消息只能夠提示格式錯誤,如果想要更為詳細(xì)的就需要我們通過js來自定義了
-
使用方法:
-
注冊事件:
oninput:輸入時
,oninvalid驗證失敗
-
設(shè)置自定義信息
dom.setCustomValidity("這里輸入提示信息");
-
示例代碼:
-
輸入時,會彈出
oninput
綁定的代碼
輸入中.png
-
驗證失敗時,會彈出
oninvalid
綁定的代碼驗證失敗.png
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action=""> email:<input type="email" name="userEmail"> <br/> tel:<input type="tel" required pattern="[0-9]{3}" id="telInput"> <br/> <input type="submit"> </form> </body> </html> <script> var telInput = document.getElementById("telInput"); // 正在輸入時 telInput.oninput=function () { this.setCustomValidity("請正確輸入哦"); } // 驗證失敗時 telInput.oninvalid=function(){ this.setCustomValidity("請不要輸入火星的手機號好嗎?"); }; </script>
總結(jié)
-
優(yōu)點:
-
html5自帶的驗證使用便捷
-
不需要額外的js框架
-
缺點:
-
兼容性問題
-
如果想要兼容所有瀏覽器,建議使用
js驗證框架
【相關(guān)推薦】
1. 免費h5在線視頻教程
2. HTML5 完整版手冊
3. php.cn原創(chuàng)html5視頻教程