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>
運行結(jié)果:


PS:這里再為大家提供2款非常方便的正則表達式工具供大家參考使用:
JavaScript正則表達式在線測試工具:
http://tools.jb51.net/regex/javascript
正則表達式在線生成工具:
http://tools.jb51.net/regex/create_reg
更多關(guān)于jQuery相關(guān)內(nèi)容可查看本站專題:《jQuery正則表達式用法總結(jié)》、《jQuery字符串操作技巧總結(jié)》、《jQuery操作xml技巧總結(jié)》、《jQuery擴展技巧總結(jié)》、《jquery選擇器用法總結(jié)》及《jQuery常用插件及用法總結(jié)》
希望本文所述對大家jQuery程序設(shè)計有所幫助。
相關(guān)文章
javascript異步處理與Jquery deferred對象用法總結(jié)
這篇文章主要介紹了javascript異步處理與Jquery deferred對象用法,結(jié)合實例形式總結(jié)分析了jQuery異步請求處理相關(guān)操作技巧,需要的朋友可以參考下2019-06-06
jQuery實現(xiàn)的感應(yīng)鼠標(biāo)懸停圖片色彩漸顯效果
這篇文章主要介紹了jQuery實現(xiàn)的感應(yīng)鼠標(biāo)懸停圖片色彩漸顯效果,涉及jQuery中hover、find、css等方法的使用技巧,需要的朋友可以參考下2015-03-03

