Home > Web Front-end > JS Tutorial > body text

js password strength check

高洛峰
Release: 2017-01-16 09:23:26
Original
1268 people have browsed it

I have been working on a pass project recently. When entering a password in the registration module, the password strength (low, medium, high) needs to be displayed. Today I will share with you the results. The code is not as complicated as the online search and can meet general needs.

html code is as follows:

<!DOCTYPE HTML>
<html>
<head>
 <meta charset="utf-8"/>
 <title>密码强度</title>
 <style type="text/css">
 #passStrength{height:6px;width:120px;border:1px solid #ccc;padding:2px;}
 .strengthLv1{background:red;height:6px;width:40px;}
 .strengthLv2{background:orange;height:6px;width:80px;}
 .strengthLv3{background:green;height:6px;width:120px;}
 </style>
</head>
<body>
 <input type="password" name="pass" id="pass" maxlength="16"/>
 <div>
 <em>密码强度:</em>
 <div id="passStrength"></div>
 </div>
</body>
</html>
<script type="text/javascript" src="js/passwordStrength.js"></script>
<script type="text/javascript">
new PasswordStrength(&#39;pass&#39;,&#39;passStrength&#39;);
</script>
Copy after login

js code is as follows:

Copy code

function PasswordStrength(passwordID,strengthID){
 this.init(strengthID);
 var _this = this;
 document.getElementById(passwordID).onkeyup = function(){
 _this.checkStrength(this.value);
 }
};
PasswordStrength.prototype.init = function(strengthID){
 var id = document.getElementById(strengthID);
 var div = document.createElement(&#39;div&#39;);
 var strong = document.createElement(&#39;strong&#39;);
 this.oStrength = id.appendChild(div);
 this.oStrengthTxt = id.parentNode.appendChild(strong);
};
PasswordStrength.prototype.checkStrength = function (val){
 var aLvTxt = [&#39;&#39;,&#39;低&#39;,&#39;中&#39;,&#39;高&#39;];
 var lv = 0;
 if(val.match(/[a-z]/g)){lv++;}
 if(val.match(/[0-9]/g)){lv++;}
 if(val.match(/(.[^a-z0-9])/g)){lv++;}
 if(val.length < 6){lv=0;}
 if(lv > 3){lv=3;}
 this.oStrength.className = &#39;strengthLv&#39; + lv;
 this.oStrengthTxt.innerHTML = aLvTxt[lv];
};
Copy after login

Instructions for use:

1. One parameter is the id of the password input box, and the second parameter is the id of the password strength bar.

2. The password strength rules can be customized in the checkStrength method.

3. Password strength displays low, medium and high corresponding to 3 css styles (strengthLv1, strengthLv2, strengthLv3).

The above is the entire content of this article. I hope it will be helpful to everyone's learning, and I also hope that everyone will subscribe to the PHP Chinese website.

For more articles related to js password strength testing, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template