jQuery實(shí)現(xiàn)驗(yàn)證表單密碼一致性及正則表達(dá)式驗(yàn)證郵箱、手機(jī)號(hào)的方法
本文實(shí)例講述了jQuery實(shí)現(xiàn)驗(yàn)證表單密碼一致性及正則表達(dá)式驗(yàn)證郵箱、手機(jī)號(hào)的方法。
jQuery 腳本
<script type="text/javascript">
function check_password() {
if ($("#password").val() != $("#checkPWD").val()){
alert("請(qǐng)保證兩次輸入密碼的一致性!");
$("#checkPWD").focus();
}
}
function check_email() {
var reg = /\w+[@]{1}\w+[.]\w+/;
if (!reg.test($("#email").val())){
alert("請(qǐng)輸入正確的email!");
$("#email").focus();
}
}
function check_phone() {
var reg = /^1[34578]\d{9}$/;
if (!reg.test($("#phone").val())){
alert("請(qǐng)輸入正確的手機(jī)號(hào)!");
$("#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>注冊(cè)界面</title>
</head>
<body>
<center>
<h1>用戶(hù)注冊(cè)</h1>
<form action="" method="post">
<table width="400px" cellspacing="0px" cellpadding="0px" border="1px">
<tr>
<td>用戶(hù)名</td>
<td><input type="text" name="username" placeholder="用戶(hù)名為3-12位字母數(shù)字或下劃線(xiàn)組合" ></td>
</tr>
<tr>
<td>密 碼</td>
<td><input type="password" name="password" placeholder="密碼長(zhǎng)度為6-12位的純數(shù)字" id="password"></td>
</tr>
<tr>
<td>確認(rèn)密碼</td>
<td><input type="password" name="checkPWD" placeholder="密碼長(zhǎng)度為6-12位的純數(shù)字" id="checkPWD" onchange="check_password()"></td>
</tr>
<tr>
<td>手機(jī)號(hào)碼</td>
<td><input type="text" name="phone" placeholder="請(qǐng)輸入正確的手機(jī)號(hào)碼格式" id="phone" onchange="check_phone()"></td>
</tr>
<tr>
<td>郵箱</td>
<td><input type="email" name="email" placeholder="請(qǐng)輸入正確郵箱格式" id="email" onchange="check_email()" required="required"></td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<input type="submit" value="注冊(cè)">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
運(yùn)行結(jié)果:


PS:這里再為大家提供2款非常方便的正則表達(dá)式工具供大家參考使用:
JavaScript正則表達(dá)式在線(xiàn)測(cè)試工具:
http://tools.jb51.net/regex/javascript
正則表達(dá)式在線(xiàn)生成工具:
http://tools.jb51.net/regex/create_reg
更多關(guān)于jQuery相關(guān)內(nèi)容可查看本站專(zhuān)題:《jQuery正則表達(dá)式用法總結(jié)》、《jQuery字符串操作技巧總結(jié)》、《jQuery操作xml技巧總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》、《jquery選擇器用法總結(jié)》及《jQuery常用插件及用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
相關(guān)文章
jQuery實(shí)現(xiàn)的淡入淡出圖片輪播效果示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的淡入淡出圖片輪播效果,涉及jQuery基于定時(shí)器的頁(yè)面元素動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-08-08
JQuery調(diào)用綁定click事件的3種寫(xiě)法
這篇文章主要介紹了JQuery調(diào)用綁定click事件的3種寫(xiě)法,本文簡(jiǎn)潔清晰的給出3種寫(xiě)法的代碼示例,可以很方便的復(fù)制使用,需要的朋友可以參考下2015-03-03
javascript異步處理與Jquery deferred對(duì)象用法總結(jié)
這篇文章主要介紹了javascript異步處理與Jquery deferred對(duì)象用法,結(jié)合實(shí)例形式總結(jié)分析了jQuery異步請(qǐng)求處理相關(guān)操作技巧,需要的朋友可以參考下2019-06-06
jQuery實(shí)現(xiàn)的感應(yīng)鼠標(biāo)懸停圖片色彩漸顯效果
這篇文章主要介紹了jQuery實(shí)現(xiàn)的感應(yīng)鼠標(biāo)懸停圖片色彩漸顯效果,涉及jQuery中hover、find、css等方法的使用技巧,需要的朋友可以參考下2015-03-03

