Javascript: Javascript is used to evaluate the strength of passwords entered by users. Javascript tutorial
Passwords are essential tools in our daily lives and work, but an insecure password may cause unnecessary losses to us. As a website designer, if we can evaluate the security of passwords entered by users on the webpage and display the corresponding prompt information, it will be very helpful for users to set a secure password. At the same time, it also makes the website more humane and more attractive.
What is a secure password? This program is evaluated as follows.
1. If the password is less than five characters, it is considered as a weak password.
2. If the password is only composed of digits, lowercase letters, uppercase letters, or other special characters, it is considered as a weak password.
3. If the password is composed of digits, lowercase letters, uppercase letters, or other special characters, it is considered to be a moderately secure password.
4. If the password is composed of more than three types of characters: Numbers, lower-case letters, upper-case letters, or other special characters, it is considered a safe password.
The program displays different colors based on the passwords entered by the user to indicate the strength of the passwords. The specific procedures are as follows:
The following is a reference clip: // CharMode Function // Test the category of a character. Function CharMode (iN ){ If (iN> = 48 & iN <= 57) // number Return 1; If (iN> = 65 & iN <= 90) // uppercase letter Return 2; If (iN> = 97 & iN <= 122) // lower case Return 4; Else Return 8; // special characters } // BitTotal Function // Calculate the total number of modes in the current password Function bitTotal (num ){ Modes = 0; For (I = 0; I <4; I ++ ){ If (num & 1) modes ++; Num >>>= 1; } Return modes; } // CheckStrong Function // Return the password strength level Function checkStrong (sPW ){ If (sPW. length <= 4) Return 0; // The password is too short Modes = 0; For (I = 0; I // Test the category of each character and calculate the total number of modes. Modes | = CharMode (sPW. charCodeAt (I )); } Return bitTotal (Modes ); } // PwStrength Function // When the user releases the keyboard or loses the focus in the password input box, different colors are displayed based on different levels Function pwStrength (pwd ){ O_color = "# eeeeee "; Rochelle color = "# FF0000 "; M_color = "# FF9900 "; H_color = "#33CC00 "; If (pwd = null | pwd = ''){ Lcolor = Mcolor = Hcolor = O_color; } Else { S_level = checkStrong (pwd ); Switch (S_level ){ Case 0: Lcolor = Mcolor = Hcolor = O_color; Case 1: Lcolor = L_color; Mcolor = Hcolor = O_color; Break; Case 2: Lcolor = Mcolor = M_color; Hcolor = O_color; Break; Default: Lcolor = Mcolor = Hcolor = H_color; } } Document. getElementById ("strength_L"). style. background = Lcolor; Document. getElementById ("strength_M"). style. background = Mcolor; Document. getElementById ("strength_H"). style. background = Hcolor; Return; } Script |