Using Javascript to evaluate the strength of user input passwords _ Javascript tutorial

Source: Internet
Author: User
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

 
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.