javascript密碼強(qiáng)度校驗(yàn)代碼(兩種方法)
先看效果圖:
javascript密碼強(qiáng)度校驗(yàn)代碼,具體實(shí)現(xiàn)思路不多說了,請(qǐng)看下面代碼和demo。
第一種方法:
/* *密碼安全程度 *return :全部為字母或者數(shù)字,或者密碼長(zhǎng)度小于 *return : 字母數(shù)字組成,或者字母特殊字符,或者數(shù)字和特殊字符 *return : 字母和數(shù)字和特殊字符 */ String.prototype.passwordStrength=function(){ if(this.length> && this.length<=) return ; var n = (this.search(/[a-zA-Z]/) != -) ? : , n = (this.search(/[-]/) != -) ? : , n =(this.search(/[\~\`\!\@\#\$\%\^\&\*\(\)\_\+\-\=\[\]|{\}\;\'\:\"\,\.\/\<\>\?]{,}/) != -) ? : ; return n+n+n; }
demo
<!doctype html> <html> <head> <meta charset="utf-"> <title>js密碼強(qiáng)度</title> <style type="text/css"> .pw_letter{ margin-top:px; font-size: px; } .pw_letter label{float: left; margin-right:px; cursor: default; font-size: px; line-height: px;;} .pw_letter span{ float: left; display:inline-block; width:px; height:px; line-height:px; text-align:center; color:#FFF; background-color:#ccc; border-left: px solid #FFF;} .pw_letter span.pw_strength_color{ background-color:green;} </style> </head> <body> <input id="password" type="password" name="password" placeholder="密碼" onKeyUp="setPasswordStrength(this.value.trim())"> <div class="pw_letter"><label>安全程度</label> <span class="strength">弱</span> <span class="strength">中</span> <span class="strength">強(qiáng)</span> </div> <script type="text/javascript"> /* *密碼安全程度 *return :全部為字母或者數(shù)字,或者密碼長(zhǎng)度小于 *return : 字母數(shù)字組成,或者字母特殊字符,或者數(shù)字和特殊字符 *return : 字母和數(shù)字和特殊字符 */ String.prototype.passwordStrength=function(){ if(this.length> && this.length<=) return ; var n = (this.search(/[a-zA-Z]/) != -) ? : , n = (this.search(/[-]/) != -) ? : , n =(this.search(/[\~\`\!\@\#\$\%\^\&\*\(\)\_\+\-\=\[\]|{\}\;\'\:\"\,\.\/\<\>\?]{,}/) != -) ? : ; return n+n+n; } String.prototype.trim = String.prototype.trim || function(){ return this.replace(/^\s+|\s+$/g,""); } function setPasswordStrength(pwd){ var strength_span = document.getElementsByClassName("strength"); for(var i=; i<strength_span.length; i++){ strength_span.item(i).className="strength"; } for(var i=; i<pwd.passwordStrength(); i++){ document.getElementsByClassName("strength").item(i).className="strength pw_strength_color"; } } </script> </body>
第二種方法:
javascript代碼如下:
<script> function AuthPasswd(string) { if(string.length >=6) { if(/[a-zA-Z]+/.test(string) && /[0-9]+/.test(string) && /\W+\D+/.test(string)) { noticeAssign(1); }else if(/[a-zA-Z]+/.test(string) || /[0-9]+/.test(string) || /\W+\D+/.test(string)) { if(/[a-zA-Z]+/.test(string) && /[0-9]+/.test(string)) { noticeAssign(-1); }else if(/\[a-zA-Z]+/.test(string) && /\W+\D+/.test(string)) { noticeAssign(-1); }else if(/[0-9]+/.test(string) && /\W+\D+/.test(string)) { noticeAssign(-1); }else{ noticeAssign(0); } } }else{ noticeAssign(null); } } function noticeAssign(num) { if(num == 1) { $('#weak').css({backgroundColor:'#009900'}); $('#middle').css({backgroundColor:'#009900'}); $('#strength').css({backgroundColor:'#009900'}); $('#strength').html('很強(qiáng)'); $('#middle').html(''); $('#weak').html(''); }else if(num == -1){ $('#weak').css({backgroundColor:'#ffcc33'}); $('#middle').css({backgroundColor:'#ffcc33'}); $('#strength').css({backgroundColor:''}); $('#weak').html(''); $('#middle').html('中'); $('#strength').html(''); }else if(num ==0) { $('#weak').css({backgroundColor:'#dd0000'}); $('#middle').css({backgroundColor:''}); $('#strength').css({backgroundColor:''}); $('#weak').html('弱'); $('#middle').html(''); $('#strength').html(''); }else{ $('#weak').html(' '); $('#middle').html(' '); $('#strength').html(' '); $('#weak').css({backgroundColor:''}); $('#middle').css({backgroundColor:''}); $('#strength').css({backgroundColor:''}); } } </script>
以上通過兩種方法介紹了javascript密碼強(qiáng)度校驗(yàn)代碼,希望對(duì)大家有所幫助。
- JS正則表達(dá)式驗(yàn)證密碼強(qiáng)度
- 原生js實(shí)現(xiàn)密碼強(qiáng)度驗(yàn)證功能
- JavaScript實(shí)現(xiàn)密碼強(qiáng)度實(shí)時(shí)驗(yàn)證
- js如何驗(yàn)證密碼強(qiáng)度
- js驗(yàn)證密碼強(qiáng)度解析
- JS 密碼強(qiáng)度校驗(yàn)的正則表達(dá)式(簡(jiǎn)單且好用)
- js檢驗(yàn)密碼強(qiáng)度(低中高)附圖
- js檢測(cè)用戶輸入密碼強(qiáng)度
- js密碼強(qiáng)度實(shí)時(shí)檢測(cè)代碼
- js判斷密碼強(qiáng)度的方法
相關(guān)文章
Layui table field初始化加載時(shí)進(jìn)行隱藏的方法
今天小編就為大家分享一篇Layui table field初始化加載時(shí)進(jìn)行隱藏的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09JavaScript實(shí)現(xiàn)定時(shí)器循環(huán)展示數(shù)組
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)定時(shí)器循環(huán)展示數(shù)組,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09純JavaScript實(shí)現(xiàn)猜數(shù)字游戲
這篇文章主要為大家詳細(xì)介紹了純JavaScript實(shí)現(xiàn)猜數(shù)字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08使用JavaScript動(dòng)態(tài)設(shè)置樣式實(shí)現(xiàn)代碼(2)
使用onmouseover和onmouseout事件實(shí)現(xiàn)不同的效果而且是使用js動(dòng)態(tài)實(shí)現(xiàn),本文有利于鞏固你js與css方面的知識(shí),感興趣的你可以了解下哦,希望本文對(duì)你有所幫助2013-01-01