Javascript password strength verification rules, scoring, and verification (front-end code is provided, and backend code can be translated according to the strength Rules)

Source: Internet
Author: User

Preface:

Password strength is a common function and is relatively simple. It is mainly about how to develop this strength rule. Now, you need to verify the password strength. The previous verification is relatively simple and cannot meet your needs. Now you need a password strength verification that can be flexibly changed and can be configured at multiple levels, so we designed the following stuff. Before the design, I also referred to the more mature strength rules, which are similar. Almost all of them adopted the scoring mechanism to control the password strength rules, which is highly configurable and flexible. I wanted to use it directly, but I found that it was relatively old. Some of them were not suitable for the company's development needs. Maybe this is simple, so no one has to update and write new code, so I designed the rules and wrote the code myself.

Implementation:

Principle:
Using the scoring mechanism, scores are divided into three categories (basic score, plus points, and minus points). The base score is obtained first, and the final total score is calculated after the score is deducted.
  
Rules:
The password can be of the following type: uppercase letters, lowercase letters, and special characters ).
It can be divided into the password length. The password can be 1 minute and more than 18 characters are all 18 minutes. The password contains an input type, with a base score of 4 points.
If the total number of passwords is greater than or equal to 2, the total number is 2. If the total number is greater than or equal to 5, the total number is 4 points.
If a single type of characters that have been repeatedly repeated consecutively, the score is reduced by 1 point at a time.
Total score: 50.
0 ~ 10 points: unqualified (weak)
11 ~ 20 points: Average
21 ~ 30 minutes: Medium
31 ~ 40 points: strong
41 ~ 50 points: Security
* The score range can be adjusted and matched freely. In fact, the entire scoring rule can be modified as needed.

Code:
Copy codeThe 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
1 @ dA = 20 points
111111 = 9 points
Abcdef1 = 19
Abcd12 = 18
Abc123 = 18
Ab123A = 22
AA12j @ = 26 points
Aasdfkjjsjjj = 16 points
111111111 dsfskjjkjeh = 25
1111 dsfskjjkjeh = 25
1231kb #4 ktSF! T @ s ^ j # hkWH = 50 points
Skhk3293ks = 24
Sfh #4 hHdk = 29 points
Bure12 # sk = 27 points
A @ s @ dk23 = 26 points
BruceLi @ 09kt = 34
Ce @ Li1 = 24
END
This is the end. You are welcome to discuss this scoring rule. You can also write your own rules and code to facilitate your research and exchange, the code must be constantly maintained and updated so that we can continue to advance on the script of our predecessors.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.