CoderZone.org

Category: >> JavaScript >> Password Strength Tester Bookmark and Share

<< lastnext >>

Snippet Name: Password Strength Tester

Description: This script evaluates a password by checking:

* Length
* Amount/Existence of Numbers
* Capital/Lowercase Ratio (for case-sensitive passwords - as nearly all are)

Comment: (none)

Author: CoderZone
Language: JAVASCRIPT
Highlight Mode: JAVASCRIPT
Last Modified: March 01st, 2009

< s c r ipt type="text/javascript">
function PassWord () {
  a=document.getElementById('Password').value ;
  b=0;
  PassWordStrength(a,b) ;
}
function PassWordStrength (a,b) {
  //Awards Points for Length
  if (a.length<12) {
   c=a.length/2 ; l=c.toFixed(0) ; g=0 ;
   while (g<l) {
     b=b+1 ; g=g+1 ;
   }     
  }
  else { b=b+6 ; }
  //This section awards points for existence and number of numbers in the Password Field
  numTest=new RegExp("[0-9]","g") ;
  numVal=1 ; c="" ;
  while (numVal!=null) {
    numVal=numTest.exec(a) ; c=c+numVal ;
  }
  g=0 ; d="" ;
  if (c.length-4<2) { b=b+0 ; }
  else if (c.length-4<4) { b=b+2 ; }
  else if (c.length-4<8) { b=b+4 ; }
  else { b=b+6 }
  //The final evaluating section will add points for the ratio of Capitalised/LowerCase
  capTest=new RegExp("[A-Z]","g") ;
  capVal=1 ; c="" ;  Total="" ;
  while (capVal!=null) {
    capVal=capTest.exec(a) ; c=c+capVal ; Total=Total+capVal
  }
  lowerTest=new RegExp("[a-z]","g") ;
  lowerVal=1 ; d="" ;
  while (lowerVal!=null) {
    lowerVal=lowerTest.exec(a) ; d=d+lowerVal ; Total=Total+lowerVal
  }
  Per=Total.length-8 ; Perc=Per/100 ; Percent=Perc*1 ; Cap=c.length-4 ; Capo=Percent*Cap ; Caps=Capo.toFixed(0) ; Low=d.length-4 ; Lowe=Percent*Low ; Lower=Lowe.toFixed(0) ; difference=Caps-Lower;
  if (difference<60) {
    dif=difference/10 ; diff=dif.toFixed(0) ; diff=diff-1 ; g=0 ;
    while (g<diff) {
      g=g+1 ; b=b+1 ;
    }
  }
  else { b=b+6 }   
  //This section converts the points gained into a bar
  f=0 ; Bar="" ;
  while (f<b*2) { f=f+1 ; Bar=Bar+"_" ; }
  //And then the Bar into Wording with the Points next to it
  if (Bar.length&#609;) { StrengthFinal="<b><font color='red'>WEAK</font></b> Points:"+b*2 ; Bar="<font color='red'>"+Bar+"</font>" ;}
  else if (Bar.length&#6018;) { StrengthFinal="<b><font color='CC6600'>MODERATE</font></b>   Points:"+b*2; Bar="<font color='CC6600'>"+Bar+"</font>" ;}
  else if (Bar.length&#6024;) { StrengthFinal="<b><font color='FF9933'>MODERATELY-STRONG</font></b>   Points:"+b*2; Bar="<font color='FF9933'>"+Bar+"</font>" ;}
  else { StrengthFinal="<b><font color='green'>STRONG</font></b>   Points:"+b*2; Bar="<font color='green'>"+Bar+"</font>" }
  //The area below defines where the information about the password is put.
  document.getElementById('Strength Box').innerHTML=StrengthFinal ;
  document.getElementById('Strength Visual').innerHTML=Bar ;
}
</script>
 
<body><div style="background-color:FFF5CF; position: absolute; width: 461px; height: 36px; z-index: 1; left: 11px; top: 14px" id="PassWord Field">
<div style="position: absolute; width: 272px; height: 6px; z-index: 3; left: 172px; top: 14px" id="Strength Visual">
    </div>
<div style="position: absolute; width: 271px; height: 12px; z-index: 2; left: 173px; top: 3px" id="Strength Box">
   <font id="Display" size="2">Password Strength:</font></div>
<div style="position: absolute; width: 161px; height: 11px; z-index: 1; left: 6px; top: 7px" id="EntryBox">
<input onkeypress='Javascript:PassWord()' type="password" id="Password"></div>
 </div>
</body>
 
 
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 18