Passwords have become an indispensable tool in our lives and work, but an unsafe password may cause us unnecessary losses. As website designers, if we can conduct a security assessment on the password entered by the user on the web page and display the corresponding prompt information, it will be of great help to the user to set a secure password. At the same time, it also makes the website more user-friendly and attractive.
What is a secure password? This program evaluates it in the following way.
1. If the password is less than 5 characters, it is considered a weak password.
2. If the password only consists of one of numbers, lowercase letters, uppercase letters or other special symbols, it is considered a weak password.
3. If the password consists of two types of numbers, lowercase letters, uppercase letters or other special symbols, it is considered a moderately secure password.
4. If the password consists of more than three types of numbers, lowercase letters, uppercase letters or other special symbols, it is considered a relatively secure password.
The specific program is as follows (demo address: http://www.netInter.cn/reg):
<script language=javascript>
//Program design: Global Wanwei, specializing in domain name registration and virtual hosting services
//Website: http://www.netInter.cn
//This program is an original program of Global Wanwei. If you need to reprint it, please indicate the URL and source. Thank you.
//The above information is an integral part of the article text, so if you want to reprint this article, you must retain the above information.
//CharMode function
//Test which category a character belongs to.
function CharMode(iN){
if (iN>=48 && iN <=57) //number
return 1;
if (iN>=65 && iN <=90) //uppercase letters
return 2;
if (iN>=97 && iN <=122) //lowercase
return 4;
else
return 8; //Special characters
}
//bitTotal function
//Calculate how many modes there are 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; //Password is too short
Modes=0;
for (i=0;i<sPW.length;i++){
//Test the category of each character and count how many patterns there are.
Modes|=CharMode(sPW.charCodeAt(i));
}
return bitTotal(Modes);
}
//pwStrength function
//When the user releases the keyboard or the password input box loses focus, different colors are displayed according to different levels
function pwStrength(pwd){
O_color="#eeeeee";
L_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>
<form name=form1 action="" >
Enter password:<input type=password size=10 onKeyUp=pwStrength(this.value) onBlur=pwStrength(this.value)>
<br>Password Strength:
<table width="217" border="1" cellspacing="0" cellpadding="1" bordercolor="#cccccc" height="23" style='display:inline'>
<tr align="center" bgcolor="#eeeeee">
<td width="33%" id="strength_L">Weak</td>
<td width="33%" id="strength_M">Medium</ td>
<td width="33%" id="strength_H">Strong</td>
</tr>
</table>
</form>