JavaScript determines password strength (self-write code) _javascript tips

Source: Internet
Author: User
Password strength prompts, such as Youku, are often visible when registering on some sites:

Today I did a JS password strength judgment, the effect is as follows:

Here's the code:
Copy Code code as follows:

<title>js Judgment Password Strength </title>
<script language=javascript>
Determine the type of password entered
function Charmode (in) {
if (in>=48 && in <=57)//number
return 1;
if (in>=65 && in <=90)//Uppercase
return 2;
if (in>=97 && in <=122)//lowercase
return 4;
Else
return 8;
}
Bittotal function
Calculate password mode
function Bittotal (num) {
modes=0;
for (i=0;i<4;i++) {
if (num & 1) modes++;
num>>>=1;
}
return modes;
}
Return strength level
function Checkstrong (SPW) {
if (spw.length<=4)
return 0; Password is too short
modes=0;
for (i=0;i<spw.length;i++) {
Password mode
Modes|=charmode (Spw.charcodeat (i));
}
Return Bittotal (Modes);
}

Display Color
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>
<body>
<form name=form1 action= "" >
<table width= "border=" 0 "cellpadding=2" bordercolor= "#eeeeee" style= ' Display:inline ' >
<tr>
&LT;TD width= "40%" align= "right" > Password:</td>
&LT;TD colspan= "3" align= "left" >
<input Type=password size=20 onkeyup=pwstrength (this.value) onblur=pwstrength (this.value) >
</td>
</tr>
&LT;TR align= "center" >
&LT;TD width= "40%" align= "right" > Password strength:</td>
&LT;TD width= "20%" id= "strength_l" bgcolor= "#f5f5f5" > Weak </td>
&LT;TD width= "20%" id= "Strength_m" bgcolor= "#f5f5f5" > </td>
&LT;TD width= "20%" id= "Strength_h" bgcolor= "#f5f5f5" > Strong </td>
</tr>
</table>
</form>
</body>
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.