JavaScript正則驗(yàn)證密碼強(qiáng)弱度的實(shí)現(xiàn)方法
密碼強(qiáng)弱度分析
密碼由數(shù)字,字母,特殊符號(hào)組成
密碼: 只有數(shù)字- 或者是只有字母,或者是只有特殊符號(hào)——1級(jí):弱 兩兩組合: 數(shù)字和字母, 數(shù)字和特殊符號(hào), 字母和特殊符號(hào)——2級(jí):中 三者都有: 數(shù)字和字母和特殊符號(hào)——3級(jí):強(qiáng)代碼版本一:基本
<!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>密碼強(qiáng)度:</b> <em id='strength'></em> <div class='strengthLv0'></div> </div></div><script> function my$(id) { return document.getElementById(id); }<script> //獲取文本框注冊(cè)鍵盤抬起事件 my$('password').onkeyup=function () { //每次鍵盤抬起都要獲取文本框中的內(nèi)容,驗(yàn)證文本框中有什么東西,得到一個(gè)級(jí)別,然后下面的div顯示對(duì)應(yīng)的顏色 //如果密碼的長度是小于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'; } }; //給我密碼,我返回對(duì)應(yīng)的級(jí)別 function getLvl(password) { var lvl=0;//默認(rèn)是0級(jí) //密碼中是否有數(shù)字,或者是字母,或者是特殊符號(hào) if(/[0-9]/.test(password)){ lvl++; } //判斷密碼中有沒有字母 if(/[a-zA-Z]/.test(password)){ lvl++; } //判斷密碼中有沒有特殊符號(hào) if(/[^0-9a-zA-Z_]/.test(password)){ lvl++; } return lvl;//1 3 }</script></body></html>
上面代碼有點(diǎn)冗余,我們對(duì)其進(jìn)行升級(jí)改寫
版本二:升級(jí)
<!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>密碼強(qiáng)度:</b> <em id='strength'></em> <div class='strengthLv0'></div> </div></div><!-- <script src='http://m.lshqa.cn/bcjs/common.js'></script> --><script> function my$(id) { return document.getElementById(id); } //獲取文本框注冊(cè)鍵盤抬起事件 my$('password').onkeyup=function () { //每次鍵盤抬起都要獲取文本框中的內(nèi)容,驗(yàn)證文本框中有什么東西,得到一個(gè)級(jí)別,然后下面的div顯示對(duì)應(yīng)的顏色 my$('strengthLevel').className='strengthLv'+(this.value.length>=6?getLvl(this.value) :0); }; //給我密碼,我返回對(duì)應(yīng)的級(jí)別 function getLvl(password) { var lvl=0;//默認(rèn)是0級(jí) //密碼中是否有數(shù)字,或者是字母,或者是特殊符號(hào) if(/[0-9]/.test(password)){ lvl++; } //判斷密碼中有沒有字母 if(/[a-zA-Z]/.test(password)){ lvl++; } //判斷密碼中有沒有特殊符號(hào) if(/[^0-9a-zA-Z_]/.test(password)){ lvl++; } return lvl;//最小的值是1,最大值是3 }</script></body></html>
到此這篇關(guān)于JavaScript正則驗(yàn)證密碼強(qiáng)弱度的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)JavaScript正則密碼強(qiáng)弱度內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法2. PHP腳本的10個(gè)技巧(8)3. IntelliJ IDEA設(shè)置背景圖片的方法步驟4. IntelliJ IDEA配置Tomcat服務(wù)器的方法5. IntelliJ IDEA導(dǎo)出項(xiàng)目的方法6. python中復(fù)數(shù)的共軛復(fù)數(shù)知識(shí)點(diǎn)總結(jié)7. Django中如何使用Channels功能8. IntelliJ IDEA調(diào)整字體大小的方法9. jsp網(wǎng)頁實(shí)現(xiàn)貪吃蛇小游戲10. idea自定義快捷鍵的方法步驟
