JavaScript正則驗證密碼強弱度的實現方法
密碼強弱度分析
密碼由數字,字母,特殊符號組成
密碼: 只有數字- 或者是只有字母,或者是只有特殊符號——1級:弱 兩兩組合: 數字和字母, 數字和特殊符號, 字母和特殊符號——2級:中 三者都有: 數字和字母和特殊符號——3級:強代碼版本一:基本
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Document</title></head><style type='text/css'> #dv{ width: 300px; height:200px; position: absolute; left:100px; top:100px; } .strengthLv0 { height: 6px; width: 120px; border: 1px solid #ccc; padding: 2px; } .strengthLv1 { background: red; height: 6px; width: 40px; border: 1px solid #ccc; padding: 2px; } .strengthLv2 { background: orange; height: 6px; width: 80px; border: 1px solid #ccc; padding: 2px; } .strengthLv3 { background: green; height: 6px; width: 120px; border: 1px solid #ccc; padding: 2px; }</style><body><div id='dv'> <label for='password'>密碼</label> <input type='text' maxlength='16'> <div> <b>密碼強度:</b> <em id='strength'></em> <div class='strengthLv0'></div> </div></div><script> function my$(id) { return document.getElementById(id); }<script> //獲取文本框注冊鍵盤抬起事件 my$('password').onkeyup=function () { //每次鍵盤抬起都要獲取文本框中的內容,驗證文本框中有什么東西,得到一個級別,然后下面的div顯示對應的顏色 //如果密碼的長度是小于6的,沒有必要判斷 if(this.value.length>=6){ var lvl=getLvl(this.value); if(lvl==1){ //弱 my$('strengthLevel').className='strengthLv1'; }else if(lvl==2){ my$('strengthLevel').className='strengthLv2'; }else if(lvl==3){ my$('strengthLevel').className='strengthLv3'; }else{ my$('strengthLevel').className='strengthLv0'; } }else{ my$('strengthLevel').className='strengthLv0'; } }; //給我密碼,我返回對應的級別 function getLvl(password) { var lvl=0;//默認是0級 //密碼中是否有數字,或者是字母,或者是特殊符號 if(/[0-9]/.test(password)){ lvl++; } //判斷密碼中有沒有字母 if(/[a-zA-Z]/.test(password)){ lvl++; } //判斷密碼中有沒有特殊符號 if(/[^0-9a-zA-Z_]/.test(password)){ lvl++; } return lvl;//1 3 }</script></body></html>
上面代碼有點冗余,我們對其進行升級改寫
版本二:升級
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Document</title></head><style type='text/css'> #dv{ width: 300px; height:200px; position: absolute; left:100px; top:100px; } .strengthLv0 { height: 6px; width: 120px; border: 1px solid #ccc; padding: 2px; } .strengthLv1 { background: red; height: 6px; width: 40px; border: 1px solid #ccc; padding: 2px; } .strengthLv2 { background: orange; height: 6px; width: 80px; border: 1px solid #ccc; padding: 2px; } .strengthLv3 { background: green; height: 6px; width: 120px; border: 1px solid #ccc; padding: 2px; }</style><body><div id='dv'> <label for='password'>密碼</label> <input type='text' maxlength='16'><!--課外話題--> <div> <b>密碼強度:</b> <em id='strength'></em> <div class='strengthLv0'></div> </div></div><!-- <script src='http://www.aoyou183.cn/bcjs/common.js'></script> --><script> function my$(id) { return document.getElementById(id); } //獲取文本框注冊鍵盤抬起事件 my$('password').onkeyup=function () { //每次鍵盤抬起都要獲取文本框中的內容,驗證文本框中有什么東西,得到一個級別,然后下面的div顯示對應的顏色 my$('strengthLevel').className='strengthLv'+(this.value.length>=6?getLvl(this.value) :0); }; //給我密碼,我返回對應的級別 function getLvl(password) { var lvl=0;//默認是0級 //密碼中是否有數字,或者是字母,或者是特殊符號 if(/[0-9]/.test(password)){ lvl++; } //判斷密碼中有沒有字母 if(/[a-zA-Z]/.test(password)){ lvl++; } //判斷密碼中有沒有特殊符號 if(/[^0-9a-zA-Z_]/.test(password)){ lvl++; } return lvl;//最小的值是1,最大值是3 }</script></body></html>
到此這篇關于JavaScript正則驗證密碼強弱度的實現方法的文章就介紹到這了,更多相關JavaScript正則密碼強弱度內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
