Objective
Password, if set too simple, it is easy to be compromised, so many sites will set the password requirements are very strict, usually letters, numbers, characters 3 selected 2, case-sensitive. For passwords that are too simple to set, give them a wrong hint. or the password level (Low medium high) display, let the user set the advanced password. So how to use JS to achieve it?
Code
Links: https://gist.github.com/xuanfeng/a44f20cb4569d5b4cd5e
1234567891011121314151617181920212223242526272829 |
function passwordlevel(password) { var modes = 0 for (i = Span class= "CRAYON-CN" >0; i < password. Length; i++) { Modes |= charmode(password. charCodeAt(i)); } return bittotal(Modes); //charmode function function charmode(in) { if (in >=) && in <= 57< c17>)//Digital return 1; if (in >= && in <= 90< c17>) //capital Letter return 2; If ((Inch >= 97 && iN <= 122 | | (in >= 65 && in <= 90) //case return 4; Else return 8; //special characters } //bittotal function function bittotal(num) { modes = 0; for (i = 0; i < 4< Span class= "Crayon-sy" >; i++) { /span> if (num & 1) modes+ +; num >>>= 1; } return modes; }} |
Use normal use
Use Syntax: Passwordlevel (String)
Validation rules: Numeric, uppercase, lowercase, special characters
Function Result: Returns the number of rules contained in the password
Running Examples:
12 |
passwordlevel("123456") //return 1 passwordlevel("ABC ' 123456") //Return 4 |
Combined with Jquery.validate.js
123456789101112131415161718192021222324 |
//Add validation method: Contains at least two rules$. Validator. Addmethod ( "STRONGPSW" function (value< Span class= "Crayon-sy" >, element) { if(passwordlevel(value) = = 1){return false ; } return true }, "format not Compliant"); //start validation$(". Form"). Validate({ rules: { pwd: { required: true, //Required minlength: 6, //min. length maxlength: max. //Maximum length strongpsw: true, //Password strength }, repwd: { required: true, minlength: 6, maxlength: equalto: "#pwd" //re-fill the password to be consistent } }}); |
Password level: At least two JS implementations with letters, uppercase and lowercase numbers, and characters