JavaScript正則表達式驗證登錄實例
更新時間:2020年03月18日 14:44:22 作者:ByRisonBoy
這篇文章主要為大家詳細介紹了JavaScript正則表達式驗證登錄實例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了js正則表達式驗證登錄的具體代碼,供大家參考,具體內容如下
代碼片段:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>qq登錄模擬測試</title> </head> <body> <form name="form1" action="#" method="post"> <input type="text" placeholder="賬號" name="username" ><br><br> <input type="password" placeholder="密碼" name="password" id="pw"> <input type="button" name="" id="btn" value="點擊顯示" /> <br><br> <input name="Submit" type="button" onClick="check()" value="登錄"/> </form> </body> <!--賬號密碼驗證--> <script type="text/javascript"> function check(){ /*非空*/ if(form1.username.value==""){ alert("賬號不能為空"); form1.username.focus(); return false; } /*含非數字、首字母為0 /^[0-9]*$/*/ var number=/^(0|[1-9][0-9]*)$/; var objExp1=new RegExp(number); if(objExp1.test(form1.username.value)==false){ alert("賬號不能含非數字和首字符為0"); form1.username.focus(); return false; } /*賬號小于6位數*/ if(form1.username.value.length < 6){ alert("賬號位數過小,請輸入6-11位數字"); form1.username.focus(); return false; } /*賬號大于11位數*/ if(form1.username.value.length > 11){ alert("賬號位數過大,請輸入6-11位數字"); form1.username.focus(); return false; } /*密碼非空*/ if(form1.password.value==""){ alert("密碼不能為空"); form1.password.focus(); return false; } /*密碼含有非符號、字母、數字以外字*/ var illegal=/^[0-9a-zA-Z!~@#$%^&*()_+`\-={}:";'<>?,.\/]*$/g; var objExp2=new RegExp(illegal); if(objExp2.test(form1.password.value)==false){ alert("密碼含有非法字符"); form1.password.focus(); return false; } /*密碼小于6位數*/ if(form1.password.value.length < 6){ alert("密碼位數過小,請輸入6-16位數字"); form1.password.focus(); return false; } /*密碼大于16位數*/ if(form1.password.value.length > 16){ alert("密碼位數過大,請輸入6-16位數字"); form1.password.focus(); return false; } form1.submit(); } </script> <!--回顯密碼--> <script type="text/javascript"> window.οnlοad=function(){ var btn=document.getElementById("btn"); var password=document.getElementById("pw") btn.οnmοusedοwn=function(){ password.type="text" }; btn.οnmοuseup=btn.οnmοuseοut=function(){ password.type="password" } } </script> </html>
結果顯示:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
javascript attachEvent和addEventListener使用方法
attachEvent與addEventListener區(qū)別 適應的瀏覽器版本不同,同時在使用的過程中要注意2009-03-03GWT中復制到剪貼板 js+flash實現復制 兼容性比較好
今天看到有個Google Code的項目,叫ZeroClipboard,大意是使用flash作為媒介,將內容復制到剪貼板。這比用純javascript好,因為不同瀏覽器會出于安全的原因,有不同反應,例如IE會給出提示,有的瀏覽器不支持復制到剪貼板。2010-03-03淺談js-FCC算法Friendly Date Ranges(詳解)
下面小編就為大家?guī)硪黄獪\談js-FCC算法Friendly Date Ranges(詳解)。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04javascript小組件 原生table排序表格腳本(兼容ie firefox opera chrome)
javascript小組件 原生table排序表格腳本 兼容ie firefox opera chrome,需要的朋友可以參考下2012-07-07