Recently has been doing a pass project, inside the registration module to enter the password required to display the password strength (low to high). Today to do the effect of sharing, the code is not online search so complex, to meet the general needs.
The HTML code is as follows:
Copy Code code as follows:
<! DOCTYPE html>
<meta charset= "Utf-8"/>
<title> Password Strength </title>
<style type= "Text/css" >
#passStrength {height:6px;width:120px;border:1px solid #ccc;p adding:2px;}
. strengthlv1{background:red;height:6px;width:40px;}
. strengthlv2{background:orange;height:6px;width:80px;}
. strengthlv3{background:green;height:6px;width:120px;}
</style>
<body>
<input type= "password" name= "pass" id= "pass" maxlength= "/>"
<div class= "Pass-wrap" >
<em> Password Strength:</em>
<div id= "Passstrength" ></div>
</div>
</body>
<script type= "Text/javascript" src= "Js/passwordstrength.js" ></script>
<script type= "Text/javascript" >
New Passwordstrength (' Pass ', ' passstrength ');
</script>
The JS code is as follows:
Copy Code code as follows:
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 (' div ');
var strong = Document.createelement (' strong ');
This.ostrength = Id.appendchild (div);
This.ostrengthtxt = Id.parentNode.appendChild (strong);
};
PasswordStrength.prototype.checkStrength = function (val) {
var alvtxt = [', ' low ', ' Medium ', ' High '];
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 = ' strengthlv ' + LV;
This.oStrengthTxt.innerHTML = Alvtxt[lv];
};
Effect Chart:
Instructions for use:
1, the first parameter of the object is the ID of the password input box, and the second parameter is the ID of the password strength bar.
2, the Checkstrength method can customize the rules of password strength.
3, the intensity of the password display low and high 3 CSS styles (strengthLv1, STRENGTHLV2, StrengthLv3) respectively.