JavaScript password strength validation rules, scoring, validation (give the front-end code, back-end code can be translated according to the strength rules) _javascript tips

Source: Internet
Author: User
Objective:

Password strength is a very common function, relatively simple, mainly how to develop this strength rule. Now need to upgrade the password strength of the verification, the previous verification is relatively simple, now can not meet the demand, now need to be flexible and multi-level configurable choice of a password strength verification, so the design of the following this dongdong. Before the design also referred to the more mature intensity rules, much the same, nothing more than the mechanism to take the scoring to control the password strength rules, so configurable high, flexible. Originally wanted to use directly, but found that are relatively old, some are not suitable for the development of the company's needs, maybe this thing is relatively simple, so there is no one to update and write new code, so they have to design the rules and write code.

Realize:

Principle:
Using the scoring mechanism, scoring is divided into 3 categories (basic, plus, minus), first to find the basis for the calculation of the additional points of the part, the final minus to deduct the score is the final total.
  
Rules:
Passwords can be typed (characters, uppercase letters, lowercase letters, special characters).
The basis is divided into the length of the password, a length of one point, more than 18 characters are 18 points; The password contains an input type, and the base is divided into 4 points.
Add, a password can enter the total number of types is greater than or equal to 2, plus 2 points, if the total amount is greater than or equal to 5, plus 4 points.
The subtraction is divided into, if there is a continuous repetition of a single type of characters, repeat minus 1 points.
Total score of 50 points.
0~10 Score: Unqualified (weak)
11~20 points: General
21~30: Medium
31~40 Score: Strong
41~50 Points: Safety
* The range of points can be freely adjusted and matched, in fact, the whole scoring rules can be modified according to need

Code
Copy Code code 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 (eval ('/' + regexarr[i] + '/g ') && Pwd.match (eval ('/' + regexarr[i] + '/g ')). Length >= 2) score + = 2;
if (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 Examples:
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
1231kb#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
Here is the end, welcome everyone to discuss this scoring rules, we can also directly give their own written rules and code, so that convenient for everyone to study and exchange, the code needs to be constantly maintained and updated, so that we can stand in the predecessors of the script to continue to advance.
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.