Snap Builder Public Codes Library
Snap | Generators | Login | Browse | Search | Enter Code | Export Snap Builder Public Codes Library

 

Category: Javascript Codes Library >> Forms, Radio, Buttons, Check Box >> Password Strength Validator


Code Snippet </> Info


Snippet Name: Password Strength Validator

Description: Easily allow your visitors to create a strong password by showing a password strength validator gauge meter.
Compatible Modern Browsers:
Chrome BrowserFirefox BrowserInternet Explorer BrowserOpera BrowserSafari Browser
Example: View Code Demo

Note:

Author: CSGNetwork

Last Modified: 2015-08-26 23:07:27

Language: javascript

Highlight Mode: javascript

Copy Codes: Use Free Notepad ++
Bookmark and Share

Snap HTML Code Editor:
Paste the source code, make changes and instantly see it in live preview.
Snap HTML Code Editor


  About Copying

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <title>Password Strength Validator</title> <style> html {margin:0; padding:0;} BODY {font-family:verdana,arial,ms sans serif; font-size:100%; background-color:#ffffff; color:#000000; text-shadow:0px 1px 1px rgba(0,0,0,0.2);} a {color:#69C;text-decoration:none;} a:hover {color:#F60; text-decoration:underline;} #wrapper {margin:0; padding:15px 15px 15px 15px;} </style> </head> <body> <div id="wrapper"> <h3>Password Strength Validator</h3> <p>Simply type a password into the box below and watch the password strength validator meter gauge the crack-ability of your password.</p> <script type="text/javascript"> <!-- // Begin Password Strength Validator // == This Script Free To Use Providing This Notice Remains == // // == This Script Has Been Found In The http://snapbuilder.com Free Public Codes Library == // // == NOTICE: Though This Material May Have Been In A Public Depository, Certain Author Copyright Restrictions May Apply == // // == Created by: CSGNetwork : http://www.CSGNetwork.com : Creative Commons License == // var minimum = ""; // can set and code for this if desired var maximum = ""; // can set and code for this if desired var ComPWs = new Array('password', 'pass', '1234', '1246', '123456', '1357', 'childname', 'spousename', 'dogname', 'birthday', 'address', 'anniversary', 'weddingdate', 'carname'); // these are a few literal examples. This can be large and a second external group, such as a dictionary should also be used. var digits = "0123456789"; // change as needed - match below var lcchrs = "abcdefghijklmnopqrstuvwxyz"; // change as needed - match below var ucchrs = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // change as needed - match below var puncchrs = "!.?,;:`"; // don't use " or ' here. change as needed - match below var specchrs = "@$#*()%~<>{}[]+-_|\/^&"; // don't use " or ' here. do not duplicate characters in puncchrs. change as needed - match below var space = " "; // ASCII 32; may or may not be valid in pws. change as needed - match below // or could use as an alternate //var space = (String.fromCharCode(32)); // ASCII 32; may or may not be valid in pws. change as needed - match below var sq = (String.fromCharCode(39)); // single quote ASCII 39; may or may not be valid in pws. change as needed - match below var dq = (String.fromCharCode(34)); // double quote ASCII 34; may or may not be valid in pws. change as needed - match below function CkPW(password) { var combinations = 0; if (contains(password, digits) > 0) { combinations += 10; } if (contains(password, lcchrs) > 0) { combinations += 26; } if (contains(password, ucchrs) > 0) { combinations += 26; } if (contains(password, puncchrs) > 0) { combinations += puncchrs.length; } if (contains(password, specchrs) > 0) { combinations += specchrs.length; } if (contains(password, space) > 0) { combinations += 1; } if (contains(password, sq) > 0) { combinations += 1; } if (contains(password, dq) > 0) { combinations += 1; } // work out the total combinations var totalCombinations = Math.pow(combinations, password.length); // if the password is a common password, then everthing changes... // this is the "shortlist"; test long list if available 75K or > if (isCommonPassword(password)) { totalCombinations = 75000; // about the size of the preferred dictionary; read in correct value from list or group } // work out how long it would take to crack this (@ 200 attempts per second) var timeInSeconds = (totalCombinations / 200) / 2; // this is how many days? (there are 86,400 seconds in a day.) var timeInDays = timeInSeconds / 86400 // how long we want it to last var lifetime = 365; // how close is the time to the projected time? var percentage = timeInDays / lifetime; var friendlyPercentage = cap(Math.round(percentage * 100), 100); if (totalCombinations != 75000 && friendlyPercentage < (password.length * 5)) { friendlyPercentage += password.length * 5; } var progressBar = document.getElementById("progressBar"); progressBar.style.width = friendlyPercentage + "%"; if (percentage > 1) { // strong password progressBar.style.backgroundColor = "#3bce08"; return; } if (percentage > 0.5) { // reasonable password progressBar.style.backgroundColor = "#ffd801"; return; } if (percentage > 0.10) { // weak password progressBar.style.backgroundColor = "orange"; return; } // dangerous password! if (percentage <= 0.10) { // weak password progressBar.style.backgroundColor = "red"; return; } } function cap(number, max) { if (number > max) { return max; } else { return number; } } function isCommonPassword(password) { // will define as white background; less than desirable in most cases // however, if it just contains it, let it go as the number of characters for (i = 0; i < ComPWs.length; i++) { var commonPassword = ComPWs[i]; if (password == commonPassword) { return true; } } return false; } function contains(password, validChars) { count = 0; // possible good with 5 chrs if include 1 from 3 different groups // at least 7 if only 1 group for caution, 8 for good for (i = 0; i < password.length; i++) { var char = password.charAt(i); if (validChars.indexOf(char) > -1) { count++; // build minimum and maximum routines here if desired // as is, unneeded } } return count; } //--> </script> <table> <tr> <td>Password:</td> <td><input type="text" size="24" maxlength="50" onKeyUp="CkPW(this.value);"/></td> </tr> <tr> <td>Strength:</td> <td><div style="border:0px solid gray; width:177px; height:20px;"> <!-- set the border to >> border:1px; if you want it visible --> <div id="progressBar" style="font-size:1px; height:20px; width:0px; border:1px solid white;"> </div> </div> </td> </tr> </table> <br /><br /> <div id="row3"> [ This code example from <a href="http://snapbuilder.com/code_snippets/copy_code_snippet_235.html">Password Strength Validator Code Snippet</a> page. ] </div> </div> </body> </html>


[ Snippet Options ]

 

© 2002 -  Snap Builder Public Codes Library