本文實(shí)例講述了jquery登錄的異步驗(yàn)證操作。分享給大家供大家參考,具體如下:
創(chuàng)新互聯(lián)主要從事網(wǎng)頁(yè)設(shè)計(jì)、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、響應(yīng)式網(wǎng)站開(kāi)發(fā)、程序開(kāi)發(fā)、網(wǎng)站優(yōu)化、微網(wǎng)站、小程序定制開(kāi)發(fā)等,憑借多年來(lái)在互聯(lián)網(wǎng)的打拼,我們?cè)诨ヂ?lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了豐富的做網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、網(wǎng)絡(luò)營(yíng)銷(xiāo)經(jīng)驗(yàn),集策劃、開(kāi)發(fā)、設(shè)計(jì)、營(yíng)銷(xiāo)、管理等多方位專(zhuān)業(yè)化運(yùn)作于一體。//定義一個(gè)json var validate = { username : false, pwd : false, pwded : false, verify : false, loginUsername : false, loginPwd :false } //存儲(chǔ)錯(cuò)誤信息 var $msg = ""; //驗(yàn)證注冊(cè)表單 $(function(){ //獲取表單對(duì)象 var register = $('form[name=register'); register.submit(function(){ var isOK = validate.username && validate.pwd && validate.pwded && validate.verify; if(isOK){ return true; } //點(diǎn)擊提交按鈕依次觸發(fā)失去焦點(diǎn)再次驗(yàn)證 $('input[name=username]',register).trigger('blur'); $('input[name=pwd]',register).trigger('blur'); $('input[name=pwded]',register).trigger('blur'); $('input[name=verify]',register).trigger('blur'); return false; }); //驗(yàn)證用戶名 $('input[name=username]',register).blur(function(){ var username = $(this).val(); var span = $(this).next(); //用戶名不能為空 if(username == ''){ msg = '用戶名不能為空'; span.html(msg).addClass('error'); validate.username = false; return; } //正則判斷 if(!/^\w{2,14}$/g.test(username)){ msg = '必須是2-14個(gè)字符,字母,數(shù)字,下劃線'; span.html(msg).addClass('error'); validate.username = false; return; } //異步驗(yàn)證用戶名是否存在 $.post(APP + '/Reg/ajax_username',{username : username},function(status){ console.log(status); if(status){ msg = '通過(guò)檢測(cè)'; span.html(msg).removeClass('error'); validate.username = true; }else{ msg = '用戶名已經(jīng)存在'; span.html(msg).addClass('error'); validate.username = false; } },'json'); }); //驗(yàn)證密碼 $('input[name=pwd]',register).blur(function(){ var pwd = $(this).val(); var span = $(this).next(); //密碼不能為空 if(pwd == ''){ msg = '密碼不能為空'; span.html(msg).addClass('error'); validate.pwd = false; return; } //正則驗(yàn)證 if(!/^\w{6,20}$/g.test(pwd)){ msg = '密碼必須由6-20個(gè)字母,數(shù)字,或者下劃線組成'; span.html(msg).addClass('error'); validate.pwd = false; return; } msg = '通過(guò)檢測(cè)'; span.html(msg).removeClass('error'); validate.pwd = true; }); //確認(rèn)密碼 $('input[name=pwded]',register).blur(function(){ var pwded = $(this).val(); var span = $(this).next(); //確認(rèn)密碼 if(pwded == ''){ msg = '請(qǐng)確認(rèn)密碼'; span.html(msg).addClass('error'); validate.pwded = false; return; } //判斷密碼是否一致 if(pwded != $('input[name=pwd]',register).val()){ msg = '密碼不一致'; span.html(msg).addClass('error'); validate.pwded = false; returnl; } msg = '通過(guò)檢測(cè)'; span.html(msg).removeClass('error'); validate.pwded = true; }); //驗(yàn)證驗(yàn)證碼 $('input[name=verify]',register).blur(function(){ var verify = $(this).val(); var span = $(this).next().next(); //不能為空 if(verify == ''){ msg = '請(qǐng)輸入驗(yàn)證碼'; span.html(msg).addClass('error'); validate.verify = false; return; } //異步判斷驗(yàn)證碼 $.post(APP + '/Reg/ajax_code',{verify : verify},function(status){ if(status){ msg = '通過(guò)檢測(cè)'; span.html(msg).removeClass('error'); validate.verify = true; }else{ msg = '驗(yàn)證碼錯(cuò)誤'; span.html(msg).addClass('error'); validate.verify = false; return; } },'json'); }); //登錄form表單驗(yàn)證 var login = $('form[name=login]'); //登錄提交事件 login.submit(function(){ if(validate.loginUsername && validate.loginPwd){ return true; } //依次觸發(fā)失去焦點(diǎn)動(dòng)作 $('input[name=username]',login).trigger('blur'); $('input[name=pwd',login).trigger('blur'); return false; }); //驗(yàn)證登錄用戶名 $('input[name=username]',login).blur(function(){ var username = $(this).val(); var span = $('#login-msg'); //判斷是否為空 if(username == ''){ msg = '請(qǐng)輸入賬號(hào)'; span.html(msg).addClass('error'); validate.loginUsername = false; return; } msg = ''; span.html(msg); validate.loginUsername = true; }); //驗(yàn)證登錄密碼 $('input[name=pwd]',login).blur(function(){ var pwd = $(this).val(); var span = $('#login-msg'); //判斷登錄密碼是否為空 if(pwd == ''){ msg = '請(qǐng)輸入密碼'; span.html(msg).addClass('error'); validate.loginPwd = false; return; } var data = { username : $('input[name=username]',login).val(), pwd : pwd }; //異步驗(yàn)證 $.post(APP + '/Login/ajax_login',data,function(status){ if(status){ msg = ''; span.html(msg).removeClass('error'); validate.loginUsername = true; validate.loginPwd = true; }else{ msg = '用戶名或密碼錯(cuò)誤'; span.html(msg).addClass('error'); validate.loginUsername = false; validate.loginPwd = false; } },'json'); msg = ''; span.html(msg); validate.loginPwd = true; }); })