本文實(shí)例為大家分享了jQuery實(shí)現(xiàn)表單驗(yàn)證的具體代碼,供大家參考,具體內(nèi)容如下

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Reg</title>
<style>
.state1{
color:#aaa;
}
.state2{
color:#000;
}
.state3{
color:red;
}
.state4{
color:green;
}
</style>
<script src="jquery.js"></script>
<script>
$(function(){
var ok1=false;
var ok2=false;
var ok3=false;
var ok4=false;
// 驗(yàn)證用戶名
$('input[name="username"]').focus(function(){
$(this).next().text('用戶名應(yīng)該為3-20位之間').removeClass('state1').addClass('state2');
}).blur(function(){
if($(this).val().length >= 3 && $(this).val().length <=12 && $(this).val()!=''){
$(this).next().text('輸入成功').removeClass('state1').addClass('state4');
ok1=true;
}else{
$(this).next().text('用戶名應(yīng)該為3-20位之間').removeClass('state1').addClass('state3');
}
});
//驗(yàn)證密碼
$('input[name="password"]').focus(function(){
$(this).next().text('密碼應(yīng)該為6-20位之間').removeClass('state1').addClass('state2');
}).blur(function(){
if($(this).val().length >= 6 && $(this).val().length <=20 && $(this).val()!=''){
$(this).next().text('輸入成功').removeClass('state1').addClass('state4');
ok2=true;
}else{
$(this).next().text('密碼應(yīng)該為6-20位之間').removeClass('state1').addClass('state3');
}
});
//驗(yàn)證確認(rèn)密碼
$('input[name="repass"]').focus(function(){
$(this).next().text('輸入的確認(rèn)密碼要和上面的密碼一致,規(guī)則也要相同').removeClass('state1').addClass('state2');
}).blur(function(){
if($(this).val().length >= 6 && $(this).val().length <=20 && $(this).val()!='' && $(this).val() == $('input[name="password"]').val()){
$(this).next().text('輸入成功').removeClass('state1').addClass('state4');
ok3=true;
}else{
$(this).next().text('輸入的確認(rèn)密碼要和上面的密碼一致,規(guī)則也要相同').removeClass('state1').addClass('state3');
}
});
//驗(yàn)證郵箱
$('input[name="email"]').focus(function(){
$(this).next().text('請(qǐng)輸入正確的EMAIL格式').removeClass('state1').addClass('state2');
}).blur(function(){
if($(this).val().search(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/)==-1){
$(this).next().text('請(qǐng)輸入正確的EMAIL格式').removeClass('state1').addClass('state3');
}else{
$(this).next().text('輸入成功').removeClass('state1').addClass('state4');
ok4=true;
}
});
//提交按鈕,所有驗(yàn)證通過(guò)方可提交
$('.submit').click(function(){
if(ok1 && ok2 && ok3 && ok4){
$('form').submit();
}else{
return false;
}
});
});
</script>
</head>
<body>
<form action='do.php' method='post' >
用 戶 名:<input type="text" name="username">
<span class='state1'>請(qǐng)輸入用戶名</span><br/><br/>
密 碼:<input type="password" name="password">
<span class='state1'>請(qǐng)輸入密碼</span><br/><br/>
確認(rèn)密碼:<input type="password" name="repass">
<span class='state1'>請(qǐng)輸入確認(rèn)密碼</span><br/><br/>
郵 箱:<input type="text" name="email">
<span class='state1'>請(qǐng)輸入郵箱</span><br/><br/>
<a href="javascript:;" rel="external nofollow" ><img class='submit' type='image' src='./images/reg.gif' /></a>
</form>
</body>
</html>
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前標(biāo)題:jQuery實(shí)現(xiàn)所有驗(yàn)證通過(guò)方可提交的表單驗(yàn)證-創(chuàng)新互聯(lián)
URL標(biāo)題:http://www.chinadenli.net/article18/iiddp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、服務(wù)器托管、網(wǎng)站策劃、網(wǎng)頁(yè)設(shè)計(jì)公司、微信小程序、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容