jQuery實現(xiàn)驗證表單密碼一致性及正則表達式驗證郵箱、手機號的方法
本文實例講述了jQuery實現(xiàn)驗證表單密碼一致性及正則表達式驗證郵箱、手機號的方法。
jQuery 腳本
<script type="text/javascript"> function check_password() { if ($("#password").val() != $("#checkPWD").val()){ alert("請保證兩次輸入密碼的一致性!"); $("#checkPWD").focus(); } } function check_email() { var reg = /\w+[@]{1}\w+[.]\w+/; if (!reg.test($("#email").val())){ alert("請輸入正確的email!"); $("#email").focus(); } } function check_phone() { var reg = /^1[34578]\d{9}$/; if (!reg.test($("#phone").val())){ alert("請輸入正確的手機號!"); $("#phone").focus(); } } </script>
html 文件
<!DOCTYPE html> <html> <head> <script src="jquery1.3.2.js"></script> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>注冊界面</title> </head> <body> <center> <h1>用戶注冊</h1> <form action="" method="post"> <table width="400px" cellspacing="0px" cellpadding="0px" border="1px"> <tr> <td>用戶名</td> <td><input type="text" name="username" placeholder="用戶名為3-12位字母數(shù)字或下劃線組合" ></td> </tr> <tr> <td>密 碼</td> <td><input type="password" name="password" placeholder="密碼長度為6-12位的純數(shù)字" id="password"></td> </tr> <tr> <td>確認密碼</td> <td><input type="password" name="checkPWD" placeholder="密碼長度為6-12位的純數(shù)字" id="checkPWD" onchange="check_password()"></td> </tr> <tr> <td>手機號碼</td> <td><input type="text" name="phone" placeholder="請輸入正確的手機號碼格式" id="phone" onchange="check_phone()"></td> </tr> <tr> <td>郵箱</td> <td><input type="email" name="email" placeholder="請輸入正確郵箱格式" id="email" onchange="check_email()" required="required"></td> </tr> <tr> <td colspan="2" style="text-align:center"> <input type="submit" value="注冊"> <input type="reset" value="重置"> </td> </tr> </table> </form> </center> </body> </html>
運行結果:
PS:這里再為大家提供2款非常方便的正則表達式工具供大家參考使用:
JavaScript正則表達式在線測試工具:
http://tools.jb51.net/regex/javascript
正則表達式在線生成工具:
http://tools.jb51.net/regex/create_reg
更多關于jQuery相關內容可查看本站專題:《jQuery正則表達式用法總結》、《jQuery字符串操作技巧總結》、《jQuery操作xml技巧總結》、《jQuery擴展技巧總結》、《jquery選擇器用法總結》及《jQuery常用插件及用法總結》
希望本文所述對大家jQuery程序設計有所幫助。
相關文章
jquery實現(xiàn)點擊文字可編輯并修改保存至數(shù)據(jù)庫
網上的方法只有點擊文字編輯并保持,但是沒有完整的代碼寫怎么保存到數(shù)據(jù)庫,本例用一條sql語句保存到數(shù)據(jù)庫2014-04-04jquery對象和javascript對象即DOM對象相互轉換
對于已經是一個 DOM 對象,只需要用 $() 把DOM對象包裝起來,就可以獲得一個 jQuery 對象了,使用[index]和.get(index)可以轉為DOM對象2014-08-08jQuery向webApi提交post json數(shù)據(jù)
這篇文章主要介紹了jQuery向webApi提交post json數(shù)據(jù)的方法,非常不錯,需要的的朋友參考下2017-01-01jQuery+Datatables實現(xiàn)表格批量刪除功能【推薦】
這篇文章主要介紹了jQuery+Datatables實現(xiàn)表格批量刪除功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-10-10jQuery1.9.1源碼分析系列(十六)ajax之ajax框架
這篇文章主要介紹了jQuery1.9.1源碼分析系列(十六)ajax之ajax框架 的相關資料,需要的朋友可以參考下2015-12-12基于jQuery Bar Indicator 插件實現(xiàn)進度條展示效果
這篇文章主要介紹了基于jQuery Bar Indicator 插件實現(xiàn)進度條展示效果的相關資料,需要的朋友可以參考下2015-09-09jquery lazyload延遲加載技術的實現(xiàn)原理分析
懶加載技術(簡稱lazyload)并不是新技術,它是js程序員對網頁性能優(yōu)化的一種方案。lazyload的核心是按需加載。在大型網站中都有l(wèi)azyload的身影,例如谷歌的圖片搜索頁,迅雷首頁,淘寶網,QQ空間等。2011-01-01