This article mainly introduces two methods for implementing at least the password level of characters including letters, uppercase and lowercase numbers in JavaScript, which can effectively detect the user password level and provide some reference value, for more information about how to use JavaScript code, see the following example. Share it with you for your reference. The details are as follows:
Preface
Password. if it is too simple to be set, it will be easily broken. Therefore, many websites set strict requirements for password settings. generally, 2 is selected for letters, numbers, and characters, case sensitive. If the password is too simple, an error is prompted. Or display the password level (low, medium, and high) so that you can set an advanced password. So how can we use JS to implement it?
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 CharMode (iN) {if (iN> = 48 & iN <= 57) // Number return 1; if (iN> = 65 & iN <= 90) // capital letter return 2; if (iN> = 97 & iN <= 122) | (iN> = 65 & iN <= 90) // case return 4; else return 8; // special character} // bitTotal function bitTotal (num) {modes = 0; for (I = 0; I <4; I ++) {if (num & 1) modes ++; num >>>= 1 ;} return modes ;}}
Use
Common use
Syntax: passwordLevel (string)
Verification rules: numbers, upper-case letters, lower-case letters, and special characters
Function result: returns the number of rules contained in the password.
Example:
PasswordLevel ("123456") // returns 1 passwordLevel ("Abc '123456") // returns 4
Combined with jquery. validate. js:
// Add verification method: contains at least two rules $. validator. addMethod ("strongPsw", function (value, element) {if (passwordLevel (value) = 1) {returnfalse;} returntrue}, "the format does not match "); // 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, failed to: "# pwd" // enter the password again }}});
For more information about password generation and strength detection, see online tools:
Online password security detection
High-strength password generator
Online generation tool for short URL
I hope this article will help you design javascript programs.