Home >Web Front-end >JS Tutorial >js test password strength (low, medium and high) with pictures_javascript skills
js test password strength (low, medium and high) with pictures_javascript skills
WBOYOriginal
2016-05-16 16:45:501369browse
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.
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]; };
Rendering:
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 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).
Statement:
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