使用正則表達(dá)式驗(yàn)證登錄頁(yè)面輸入是否符合要求
更新時(shí)間:2017年09月07日 16:11:34 作者:煙花盛典
這篇文章主要介紹了使用正則表達(dá)式驗(yàn)證登錄頁(yè)面輸入是否符合要求的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
先給大家展示下效果圖:
廢話不多說了,直接給大家貼代碼了,具體代碼如下所示:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <script src="js/jquery-1.8.0.min.js"></script> <script> $(function() { $("input[name='uname']").blur(function() { //失去焦點(diǎn) var namestr = $(this).val(); var regstr = /^[\u4e00-\u9fa5]{2,4}$/; if(!regstr.test(namestr)) { $(this).parent().next().html("用戶名必須是2-4個(gè)漢字").css("color", "red"); return false; } return true; }); $("input[name = 'uname']").focus(function() { //獲取焦點(diǎn) $(this).val(""); $(this).parent().next().html(""); }); $("input[name='pwd']").blur(function() { var pwdstr = $(this).val(); var regstr = /^\w{6}$/; if(!regstr.test(pwdstr)) { $(this).parent().next().html("密碼必須是6位數(shù)字字母下劃線").css("color", "red"); return false; } return true; }); $("input[name='pwd']").focus(function() { $(this).parent().next().html(""); }); $("input[name='birthday']").blur(function() { var birthdaystr = $(this).val(); var regstr = /^(19|20)\d{2}-(1[0-2]|0?[1-9])-(3[0-1]|2[0-9]|0?[1-9])$/; if(!regstr.test(birthdaystr)) { $(this).parent().next().html("日期格式不正確").css("color", "red"); return false; } return true; }); $("input[name='birthday']").focus(function() { $(this).parent().next().html(""); }); $("input[name='email']").blur(function(){ var emailstr = $(this).val(); var regstr = /^[\w\-]+@[a-z0-9A-Z]+(\.[a-zA-Z]{2,3}){1,2}$/; if(!regstr.test(emailstr)){ $(this).parent().next().html("郵箱格式不正確").css("color","red"); return false; } return true; }); $("input[name='email']").focus(function(){ $(this).parent().next().html(""); }); }); </script> <style> body { font-size: 12px; } #home { background-color: beige; border: solid 1px black; width: 550px; height: 185px; margin: auto; margin-top: 20px; } #head { height: 135px; } #foot { text-align: center; } .dl1 { clear: both; padding-left: 10px; } .dl1 dt { float: left; height: 30px; width: 80px; line-height: 30px; } .dl1 dd { float: left; height: 30px; line-height: 30px; /*width: 250px;*/ } #btn_res { background-image: url(img/reset.gif); width: 80px; height: 34px; } #btn_sub { background-image: url(img/submit.gif); width: 80px; height: 34px; } </style> <body> <div id="home"> <div id="head"> <form action="" method="post"> <dl class="dl1"> <dt>用戶名 : </dt> <dd class="dd1"><input type="text" value="輸入用戶名" name="uname" /></dd> <dd></dd> </dl> <dl class="dl1"> <dt>用戶密碼 : </dt> <dd class="dd1"><input type="password" name="pwd" /></dd> <dd></dd> </dl> <dl class="dl1"> <dt>出生日期 : </dt> <dd class="dd1"><input type="text" name="birthday" /></dd> <dd>yyyy-mm-dd</dd> </dl> <dl class="dl1"> <dt>用戶郵箱 : </dt> <dd><input type="text" name="email"/></dd> <dd></dd> </dl> </form> </div> <div id="foot"> <input type="submit" value="" id="btn_sub" /> <input type="reset" value="" id="btn_res" /> </div> </div> </body> </html>
總結(jié)
以上所述是小編給大家介紹的使用正則表達(dá)式驗(yàn)證登錄頁(yè)面輸入是否符合要求,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Python正則表達(dá)式的7個(gè)使用典范(推薦)
這篇文章主要介紹了Python正則表達(dá)式的7個(gè)使用典范,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11PHP中正則表達(dá)式對(duì)UNICODE字符碼的匹配方法
看到標(biāo)題是“請(qǐng)教PHP 一個(gè)正則匹配的問題”,又是正則表達(dá)式,好吧,看下,誰讓俺比較喜歡鼓搗正則呢。下面開始正題。2011-04-04正則表達(dá)式實(shí)現(xiàn)字符串每4位后自動(dòng)加空格效果(兩種方法)
本文通過兩種方法給大家介紹了正則表達(dá)式實(shí)現(xiàn)字符串每4位后自動(dòng)加空格效果,需要的朋友可以參考下2018-09-09史上最全的PHP正則表達(dá)式(手機(jī)號(hào)需要加上177-***)
正則表達(dá)式,大家在開發(fā)中應(yīng)該是經(jīng)常用到,現(xiàn)在很多開發(fā)語言都有正則表達(dá)式的應(yīng)用,比如javascript,java,.net,php等等,今天給大家介紹史上最全的PHP正則表達(dá)式(手機(jī)號(hào)需要加上177-***),一起看看吧2017-10-10