Home  >  Article  >  Web Front-end  >  JavaScript password strength verification rules, scoring, and verification (the front-end code is given, and the back-end code can be translated according to the strength rules)_javascript skills

JavaScript password strength verification rules, scoring, and verification (the front-end code is given, and the back-end code can be translated according to the strength rules)_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:27:011082browse

Foreword:

Password strength is a very common function and is relatively simple. The main thing is how to formulate this strength rule. Now we need to upgrade the password strength verification. The previous verification was relatively simple, but now it can no longer meet the needs. Now we need a password strength verification that can be flexibly changed and has multiple levels of configurable options, so we designed the following stuff. Before designing, we also referred to the more mature strength rules, which are similar. However, they all adopt a scoring mechanism to control the password strength rules, which makes them highly configurable and flexible. I originally wanted to use it directly, but I found that it was relatively old and some were not suitable for the company's development needs. Maybe this thing was relatively simple, so no one updated or wrote new code, so I designed the rules and code myself. Wrote the code.

Implementation:

Principle:
Using a scoring mechanism, scoring is divided into 3 categories (basic points, bonus points, subtraction points). The basic points are calculated first, and then the bonus points are calculated part, and finally subtract the points to be deducted to get the final total score.

Rules:
Password input types (characters, uppercase letters, lowercase letters, special characters).
The basic points are: password length, each length is one point, and any length greater than 18 characters is 18 points; if the password contains an input type, the basic points are increased by 4 points.
Bonus points: If the total number of input types of a password is greater than or equal to 2, 2 points are added. If the total number is greater than or equal to 5, 4 points are added.
Points will be deducted. If there are continuous repetitions of a single type of character, 1 point will be deducted for each repetition.
The total score is 50 points.
0 to 10 points: Unqualified (weak)
11 to 20 points: Average
21 to 30 points: Medium
31 to 40 points: Strong
41 to 50 points: Safe
*The score range can be freely adjusted and matched. In fact, the entire scoring rules can be modified as needed

Code:

Copy code The code is as follows:

function passwordGrade(pwd) {
var score = 0;
var regexArr = ['[0-9]', '[a-z]' , '[A-Z]', '[\W_]'];
var repeatCount = 0;
var prevChar = '';
//check length
var len = pwd.length;
score = len > 18 ? 18 : len;
//check type
for (var i = 0, num = regexArr.length; i < num; i ) { if (eval('/ ' regexArr[i] '/').test(pwd)) score = 4; }
//bonus point
for (var i = 0, num = regexArr.length; i < num; i ) {
if (pwd.match(eval('/' regexArr[i] '/g')) && pwd.match(eval('/' regexArr[i] '/g')).length >= 2) score = 2;
if (pwd.match(eval('/' regexArr[i] '/g')) && pwd.match(eval('/' regexArr[i] '/g')) .length >= 5) score = 2;
}
//deduction
for (var i = 0, num = pwd.length; i < num; i ) {
if ( pwd.charAt(i) == prevChar) repeatCount ;
else prevChar = pwd.charAt(i);
}
score -= repeatCount * 1;
return score;
}

Scoring example:
1111=7 points
1@dA=20 points
111111=9 points
abcdef1=19 points
abcd12=18 points
abc123=18 points
ab123A=22 points
aA12j@=26 points
aasdfkjjsjjj=16 points
111111111dsfskjjkjeh=25 points
1111dsfskjjkjeh=25 points
123 1kb#4ktSF!T@ s^j#hkWH=50 points
skhk3293ks=24 points
sfh#4hHdk=29 points
bure12#sk=27 points
a@s@dk23=26 points
bruceLi@09kt =34 points
ce@Li1=24 points
END
That’s it. Everyone is welcome to discuss this scoring rule. You can also directly give the rules and codes you have written, so To facilitate everyone’s research and communication, the code needs to be continuously maintained and updated, so that we can continue to move forward based on the scripts of our predecessors.
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