Code.GeekInterview.com
  I am new, Sign me up!
 
Code Samples Java Script
 

JavaScript Password Strength Meter


Code ResourceAuthor: srinivasaraobora  

Difficulty Level: Intermediate

Published: 3rd Jun 2009   Read: 2349 times  

Filed in: Java Script
Add Comment


 


This script is for Pass Word Strength Meter

 


Sample Code
  1. pw_strenth.js
  2.  
  3.  
  4.  
  5.  
  6. // Settings
  7. // -- Toggle to true or false, if you want to change what is checked in the password
  8. var bCheckNumbers = true;
  9. var bCheckUpperCase = true;
  10. var bCheckLowerCase = true;
  11. var bCheckPunctuation = true;
  12. var nPasswordLifetime = 365;
  13.  
  14. // Check password
  15. function checkPassword(strPassword)
  16. {
  17.         // Reset combination count
  18.         nCombinations = 0;
  19.        
  20.         // Check numbers
  21.         if (bCheckNumbers)
  22.         {
  23.                 strCheck = "0123456789";
  24.                 if (doesContain(strPassword, strCheck) > 0)
  25.                 {
  26.                         nCombinations += strCheck.length;
  27.                 }
  28.         }
  29.        
  30.         // Check upper case
  31.         if (bCheckUpperCase)
  32.         {
  33.                 strCheck = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  34.                 if (doesContain(strPassword, strCheck) > 0)
  35.                 {
  36.                         nCombinations += strCheck.length;
  37.                 }
  38.         }
  39.        
  40.         // Check lower case
  41.         if (bCheckLowerCase)
  42.         {
  43.                 strCheck = "abcdefghijklmnopqrstuvwxyz";
  44.                 if (doesContain(strPassword, strCheck) > 0)
  45.                 {
  46.                         nCombinations += strCheck.length;
  47.                 }
  48.         }
  49.        
  50.         // Check punctuation
  51.         if (bCheckPunctuation)
  52.         {
  53.                 strCheck = ";:-_=+|//?^&!.@$£#*()%~<>{}[]";
  54.                 if (doesContain(strPassword, strCheck) > 0)
  55.                 {
  56.                         nCombinations += strCheck.length;
  57.                 }
  58.         }
  59.        
  60.         // Calculate
  61.         // -- 500 tries per second => minutes
  62.         var nDays = ((Math.pow(nCombinations, strPassword.length) / 500) / 2) / 86400;
  63.  
  64.         // Number of days out of password lifetime setting
  65.         var nPerc = nDays / nPasswordLifetime;
  66.        
  67.         return nPerc;
  68. }
  69.  
  70. // Runs password through check and then updates GUI
  71. function runPassword(strPassword, strFieldID)
  72. {
  73.         // Check password
  74.         nPerc = checkPassword(strPassword);
  75.        
  76.          // Get controls
  77.         var ctlBar = document.getElementById(strFieldID + "_bar");
  78.         var ctlText = document.getElementById(strFieldID + "_text");
  79.         if (!ctlBar || !ctlText)
  80.                 return;
  81.        
  82.         // Set new width
  83.         var nRound = Math.round(nPerc * 100);
  84.         if (nRound < (strPassword.length * 5))
  85.         {
  86.                 nRound += strPassword.length * 5;
  87.         }
  88.         if (nRound > 100)
  89.                 nRound = 100;
  90.         ctlBar.style.width = nRound + "%";
  91.  
  92.         // Color and text
  93.         if (nRound > 95)
  94.         {
  95.                 strText = "Very Secure";
  96.                 strColor = "#3bce08";
  97.         }
  98.         else if (nRound > 75)
  99.         {
  100.                 strText = "Secure";
  101.                 strColor = "orange";
  102.         }
  103.         else if (nRound > 50)
  104.         {
  105.                 strText = "Mediocre";
  106.                 strColor = "#ffd801";
  107.         }
  108.         else
  109.         {
  110.                 strColor = "red";
  111.                 strText = "Insecure";
  112.         }
  113.         ctlBar.style.backgroundColor = strColor;
  114.         ctlText.innerHTML = "<span style='color: " + strColor + ";'>" + strText + "</span>";
  115. }
  116.  
  117. // Checks a string for a list of characters
  118. function doesContain(strPassword, strCheck)
  119.  {
  120.         nCount = 0;
  121.  
  122.         for (i = 0; i < strPassword.length; i++)
  123.         {
  124.                 if (strCheck.indexOf(strPassword.charAt(i)) > -1)
  125.                 {
  126.                         nCount++;
  127.                 }
  128.         }
  129.  
  130.         return nCount;
  131. }
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. pwdsrength.html
  140.  
  141.  
  142.  
  143.  
  144.  
  145. // Settings
  146. // -- Toggle to true or false, if you want to change what is checked in the password
  147. var bCheckNumbers = true;
  148. var bCheckUpperCase = true;
  149. var bCheckLowerCase = true;
  150. var bCheckPunctuation = true;
  151. var nPasswordLifetime = 365;
  152.  
  153. // Check password
  154. function checkPassword(strPassword)
  155. {
  156.         // Reset combination count
  157.         nCombinations = 0;
  158.        
  159.         // Check numbers
  160.         if (bCheckNumbers)
  161.         {
  162.                 strCheck = "0123456789";
  163.                 if (doesContain(strPassword, strCheck) > 0)
  164.                 {
  165.                         nCombinations += strCheck.length;
  166.                 }
  167.         }
  168.        
  169.         // Check upper case
  170.         if (bCheckUpperCase)
  171.         {
  172.                 strCheck = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  173.                 if (doesContain(strPassword, strCheck) > 0)
  174.                 {
  175.                         nCombinations += strCheck.length;
  176.                 }
  177.         }
  178.        
  179.         // Check lower case
  180.         if (bCheckLowerCase)
  181.         {
  182.                 strCheck = "abcdefghijklmnopqrstuvwxyz";
  183.                 if (doesContain(strPassword, strCheck) > 0)
  184.                 {
  185.                         nCombinations += strCheck.length;
  186.                 }
  187.         }
  188.        
  189.         // Check punctuation
  190.         if (bCheckPunctuation)
  191.         {
  192.                 strCheck = ";:-_=+|//?^&!.@$£#*()%~<>{}[]";
  193.                 if (doesContain(strPassword, strCheck) > 0)
  194.                 {
  195.                         nCombinations += strCheck.length;
  196.                 }
  197.         }
  198.        
  199.         // Calculate
  200.         // -- 500 tries per second => minutes
  201.         var nDays = ((Math.pow(nCombinations, strPassword.length) / 500) / 2) / 86400;
  202.  
  203.         // Number of days out of password lifetime setting
  204.         var nPerc = nDays / nPasswordLifetime;
  205.        
  206.         return nPerc;
  207. }
  208.  
  209. // Runs password through check and then updates GUI
  210. function runPassword(strPassword, strFieldID)
  211. {
  212.         // Check password
  213.         nPerc = checkPassword(strPassword);
  214.        
  215.          // Get controls
  216.         var ctlBar = document.getElementById(strFieldID + "_bar");
  217.         var ctlText = document.getElementById(strFieldID + "_text");
  218.         if (!ctlBar || !ctlText)
  219.                 return;
  220.        
  221.         // Set new width
  222.         var nRound = Math.round(nPerc * 100);
  223.         if (nRound < (strPassword.length * 5))
  224.         {
  225.                 nRound += strPassword.length * 5;
  226.         }
  227.         if (nRound > 100)
  228.                 nRound = 100;
  229.         ctlBar.style.width = nRound + "%";
  230.  
  231.         // Color and text
  232.         if (nRound > 95)
  233.         {
  234.                 strText = "Very Secure";
  235.                 strColor = "#3bce08";
  236.         }
  237.         else if (nRound > 75)
  238.         {
  239.                 strText = "Secure";
  240.                 strColor = "orange";
  241.         }
  242.         else if (nRound > 50)
  243.         {
  244.                 strText = "Mediocre";
  245.                 strColor = "#ffd801";
  246.         }
  247.         else
  248.         {
  249.                 strColor = "red";
  250.                 strText = "Insecure";
  251.         }
  252.         ctlBar.style.backgroundColor = strColor;
  253.         ctlText.innerHTML = "<span style='color: " + strColor + ";'>" + strText + "</span>";
  254. }
  255.  
  256. // Checks a string for a list of characters
  257. function doesContain(strPassword, strCheck)
  258.  {
  259.         nCount = 0;
  260.  
  261.         for (i = 0; i < strPassword.length; i++)
  262.         {
  263.                 if (strCheck.indexOf(strPassword.charAt(i)) > -1)
  264.                 {
  265.                         nCount++;
  266.                 }
  267.         }
  268.  
  269.         return nCount;
  270. }
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
Copyright GeekInterview.com


Next Article: JavaScript Pop-up Window


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Comment


Members Please Login

Name:  Email: (Optional. Used for Notification)

Title:
 
Comment:
Validation Code: <=>  (Enter this code in text box)





Popular Coders

# Coder NameHits
1. srinivasaraobora5409
2. Durga Prasad3859
3. chowsys2582
4. iamdvr1864

Active Coders

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

Refined Tags

 

Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact  

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape