JS evaluate password strength

Source: Internet
Author: User

Http://www.phpchina.com/14112/viewspace_28701.html
JS implementation to evaluate the password strength
<SCRIPT type = " Text/JavaScript " >
// Charmode Function
// Test the type of a character.
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 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
// Returns the password strength level.

Function Checkstrong (spw) {
If (Spw. Length <= 4 )
Return   0 ; // The password is too short.
Modes = 0 ;
For (I = 0 ; I < Spw. length; I ++ ) {
//Test the category of each character and calculate the total number of modes.
Modes| =Charmode (spw. charcodeat (I ));
}  

ReturnBittotal (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 = Rochelle 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 ;
}
  </ SCR limit PT >

< Form Name = Form1 Action = ""   >  
Enter the 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 >

  JS class for checking the password strength
Http://www.cnblogs.com/thinhunan/archive/2006/05/14/399656.html
<SCRIPT type = "text/JavaScript">
VaR passwordstrength = {
Level: ["high, it is really high", "okay", "rely on, this is OK"],
Levelvalue: [30, 20, 0], // intensity value
Factor: [1, 2, 5], // character addition, which are letters, numbers, and others
Kindfactor: [0, 0, 10, 20], // The password consists of several numbers.
RegEx: [/[A-Za-Z]/g, // \ D/g,/[^ a-zA-Z0-9]/g] // character regular number regular other regular
}
Passwordstrength. strengthvalue = function (PWD)
{
VaR strengthvalue = 0;
VaR composedkind = 0;
For (VAR I = 0; I <this. RegEx. length; I ++)
{
VaR chars = PWD. Match (this. RegEx [I]);
If (chars! = NULL)
{
Strengthvalue + = chars. length * This. factor [I];
Composedkind ++;
}
}
Strengthvalue + = This. kindfactor [composedkind];
Return strengthvalue;
}
Passwordstrength. strengthlevel = function (PWD)
{
VaR value = This. strengthvalue (PWD );
For (VAR I = 0; I <this. levelvalue. length; I ++)
{
If (value> = This. levelvalue [I])
Return this. level [I];
}
}

Alert (passwordstrength. strengthlevel ("23 "));
Alert (passwordstrength. strengthlevel ("abcd123 "));
Alert (passwordstrength. strengthlevel ("ABCD! % 23 "));
</SCRIPT>

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.