Snippets v2

Languages
My Snippets

Password Strength Check Function Login or Register to Bookmark this snippet

Description Javascript

This function returns an associative array of values determining the strength of str. It can also determine if the password is the same as the user name, uname.

Usage:
  var strength = passwordStrength('walter','user566');

Then use the variable like:
  alert(strength.text);

Along with text, you also have strength and error attributes.

The Code Download
  1. function passwordStrength(str, uname) {
  2.  
  3.     if (!str || typeof(str) == 'undefined' || str.length < 8) {
  4.         return {strength: 0, error: 1, text:'The password is not long enough'};
  5.     } else {
  6.         var length = str.length;
  7.  
  8.         //check for a couple of bad passwords:
  9.         if (uname && str.toLowerCase() == uname.toLowerCase()) {
  10.             return{strength: 0, error:4, text:'Password cannot be the same as your Username'};
  11.         }
  12.         if (str.toLowerCase() == 'password') {
  13.             return {strength: 0, error: 3, text:'Password is too common'};
  14.         }
  15.  
  16.         //see if there are 3 consecutive chars (or more) and fail!
  17.         var consecutives = str.match(/(.)1{2}/g);
  18.         if (consecutives) {
  19.             return {strength: 0, error: 2, text:'Too many consecutive characters'};
  20.         }
  21.  
  22.         //count the number of numerics
  23.         var numbers = str.match(/d/g);
  24.         if (numbers) {
  25.             numbers = numbers.length
  26.         } else {
  27.             var numbers = 0;
  28.         }
  29.  
  30.         //count the number of uppercase chars
  31.         var uppers = str.match(/[A-Z]/g);
  32.         if (uppers) {
  33.             uppers = uppers.length
  34.         } else {
  35.             var uppers = 0;
  36.         }
  37.  
  38.         //count the number of non-alhpa-num chars
  39.         var others = str.match(/[^A-z0-9]/g);
  40.         if (others) {
  41.             others = others.length
  42.         } else {
  43.             var others = 0;
  44.         }
  45.  
  46.         //and weigh them all up.
  47.         if (others > 1 || (uppers > 1 && numbers > 1)) {
  48.             //bulletproof
  49.             return {strength: 5, error: 0, text:'Virtually Bulletproof'};
  50.         } else if ((uppers > 0 && numbers > 0) || length > 14) {
  51.             //very strong
  52.             return {strength: 4, error: 0, text:'Very Strong'};
  53.         } else if (uppers > 0 || numbers > 2 || length > 9) {
  54.             //strong
  55.             return {strength: 3, error: 0, text:'Strong'};
  56.         } else if (numbers > 1) {
  57.             //good
  58.             return {strength: 2, error: 0, text:'Fair'};
  59.         }
  60.         //fair
  61.         return {strength: 1, error: 0, text:'Weak'};
  62.     }
  63. }
Credits Contact JC

Added by JC on 18th December, 2007

Comments

There are no comments about this snippet.

Post Comment HTML is allowed

You must be registered and logged in to post a comment.

Login here to post a comment