Code.GeekInterview.com
 
Code Samples Java Script
 

JavaScript Password Strength Meter


Code ResourceAuthor: srinivasaraobora  

Difficulty Level: Intermediate

Published: 3rd Jun 2009   Read: 8998 times  

Filed in: Java Script
Add Comment


 

 

Sponsored Links


 

 

This script is for Pass Word Strength Meter

 


Sample Code
  1. pw_strenth.js
  2.  
  3. // Settings
  4. // -- Toggle to true or false, if you want to change what is checked in the password
  5. var bCheckNumbers = true;
  6. var bCheckUpperCase = true;
  7. var bCheckLowerCase = true;
  8. var bCheckPunctuation = true;
  9. var nPasswordLifetime = 365;
  10.  
  11. // Check password
  12. function checkPassword(strPassword)
  13. {
  14.         // Reset combination count
  15.         nCombinations = 0;
  16.        
  17.         // Check numbers
  18.         if (bCheckNumbers)
  19.         {
  20.                 strCheck = "0123456789";
  21.                 if (doesContain(strPassword, strCheck) > 0)
  22.                 {
  23.                         nCombinations += strCheck.length;
  24.                 }
  25.         }
  26.        
  27.         // Check upper case
  28.         if (bCheckUpperCase)
  29.         {
  30.                 strCheck = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  31.                 if (doesContain(strPassword, strCheck) > 0)
  32.                 {
  33.                         nCombinations += strCheck.length;
  34.                 }
  35.         }
  36.        
  37.         // Check lower case
  38.         if (bCheckLowerCase)
  39.         {
  40.                 strCheck = "abcdefghijklmnopqrstuvwxyz";
  41.                 if (doesContain(strPassword, strCheck) > 0)
  42.                 {
  43.                         nCombinations += strCheck.length;
  44.                 }
  45.         }
  46.        
  47.         // Check punctuation
  48.         if (bCheckPunctuation)
  49.         {
  50.                 strCheck = ";:-_=+|//?^&!.@$£#*()%~<>{}[]";
  51.                 if (doesContain(strPassword, strCheck) > 0)
  52.                 {
  53.                         nCombinations += strCheck.length;
  54.                 }
  55.         }
  56.        
  57.         // Calculate
  58.         // -- 500 tries per second => minutes
  59.         var nDays = ((Math.pow(nCombinations, strPassword.length) / 500) / 2) / 86400;
  60.  
  61.         // Number of days out of password lifetime setting
  62.         var nPerc = nDays / nPasswordLifetime;
  63.        
  64.         return nPerc;
  65. }
  66.  
  67. // Runs password through check and then updates GUI
  68. function runPassword(strPassword, strFieldID)
  69. {
  70.         // Check password
  71.         nPerc = checkPassword(strPassword);
  72.        
  73.          // Get controls
  74.         var ctlBar = document.getElementById(strFieldID + "_bar");
  75.         var ctlText = document.getElementById(strFieldID + "_text");
  76.         if (!ctlBar || !ctlText)
  77.                 return;
  78.        
  79.         // Set new width
  80.         var nRound = Math.round(nPerc * 100);
  81.         if (nRound < (strPassword.length * 5))
  82.         {
  83.                 nRound += strPassword.length * 5;
  84.         }
  85.         if (nRound > 100)
  86.                 nRound = 100;
  87.         ctlBar.style.width = nRound + "%";
  88.  
  89.         // Color and text
  90.         if (nRound > 95)
  91.         {
  92.                 strText = "Very Secure";
  93.                 strColor = "#3bce08";
  94.         }
  95.         else if (nRound > 75)
  96.         {
  97.                 strText = "Secure";
  98.                 strColor = "orange";
  99.         }
  100.         else if (nRound > 50)
  101.         {
  102.                 strText = "Mediocre";
  103.                 strColor = "#ffd801";
  104.         }
  105.         else
  106.         {
  107.                 strColor = "red";
  108.                 strText = "Insecure";
  109.         }
  110.         ctlBar.style.backgroundColor = strColor;
  111.         ctlText.innerHTML = "<span style='color: " + strColor + ";'>" + strText + "</span>";
  112. }
  113.  
  114. // Checks a string for a list of characters
  115. function doesContain(strPassword, strCheck)
  116.  {
  117.         nCount = 0;
  118.  
  119.         for (i = 0; i < strPassword.length; i++)
  120.         {
  121.                 if (strCheck.indexOf(strPassword.charAt(i)) > -1)
  122.                 {
  123.                         nCount++;
  124.                 }
  125.         }
  126.  
  127.         return nCount;
  128. }
  129.  
  130.  
  131.  
Copyright GeekInterview.com


Next Article: JavaScript Pop-up Window


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Popular Coders

# Coder NameHits
1. srinivasaraobora17887
2. Durga Prasad12332
3. chowsys6214
4. iamdvr4268
5. Kalidasan3344

Active Coders

# Coder NameCodes
1. srinivasaraobora3
2. chowsys2
3. Durga Prasad1
4. iamdvr1
5. Kalidasan1

Refined Tags