This article describes two methods of JS implementing password levels that include at least letters, upper case numbers, and characters. Share it for your reference. The details are as follows:
Preface
If the password is set too simple, it will be easily broken. Therefore, many websites set the password settings requirements quite strictly, generally 3-choose 2 letters, numbers, and characters, which are case-sensitive. For passwords that are set too simply, give an error message. Or display the password level (low, medium and high) to let the user set a high-level password. So how to use JS implementation?
The implementation code is as follows:
function passwordLevel(password) { var Modes = 0; for (i = 0; i < password.length; i++) { Modes |= CharMode(password.charCodeAt(i)); } return bitTotal(Modes); //CharMode function function CharMode(iN) { if (iN >= 48 && iN <= 57)//Number return 1; if (iN >= 65 && iN <= 90) //Uppercase return 2; if ((iN >= 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; i++) { if (num & 1) modes++; num >>>= 1; } return modes; }}use
Normal use
Use syntax: passwordLevel(string)
Verification rules: numbers, capital letters, lowercase letters, special characters
Function result: Returns the number of rules contained in the password
Running example:
passwordLevel("123456") //Return 1passwordLevel("Abc'123456") //Return 4Use in combination with jquery.validate.js:
//Add verification method: contains at least two rules $.validator.addMethod("strongPsw",function(value,element){ if(passwordLevel(value)==1){returnfalse;} returntrue},"form not conforming");//Start verification $(".form").validate({ rules:{ pwd:{ required:true, //Required minlength:6, //Minimum length maxlength:32, //Maximum length strongPsw:true, //Password strength}, repwd:{ required:true, minlength:6, maxlength:32, equalTo:"#pwd" //Fill in the password again to the same } }});For friends who are interested in password generation and strength detection, you can also refer to the online tools:
Password security online detection
High-strength password generator
Short link (short URL) online generation tool
I hope this article will be helpful to everyone's JavaScript programming.