summaryrefslogtreecommitdiff
path: root/modules/user/js/password_strength.js
blob: 5764e3329a6a6e2b2cc34d93c044e4ca5a687af8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(function($) {
  // Based on the Password Strength Indictor By Benjamin Sterling
  // http://benjaminsterling.com/password-strength-indicator-and-generator/
  $.widget("ui.user_password_strength", {
    options: {
      classes: ['g-password-strength10', 'g-password-strength20', 'g-password-strength30',
                'g-password-strength40', 'g-password-strength50', 'g-password-strength60',
                'g-password-strength70', 'g-password-strength80', 'g-password-strength90',
                'g-password-strength100']
    },

    _init: function() {
      var self = this;
      $(this.element).keyup(function() {
        var strength = self.calculateStrength(this.value);
        var index = Math.min(Math.floor(strength / 10), 10);
        $("#g-password-gauge")
          .removeAttr("class")
          .addClass("g-password-strength0")
          .addClass(self.options.classes[index]);
      }).after("<div id='g-password-gauge' class='g-password-strength0'></div>");
    },

    calculateStrength: function(value) {
      // Factor in the length of the password
      var strength = Math.min(5, value.length) * 10 - 20;
      // Factor in the number of numbers
      strength += Math.min(3, value.length - value.replace(/[0-9]/g,"").length) * 10;
      // Factor in the number of non word characters
      strength += Math.min(3, value.length - value.replace(/\W/g,"").length) * 15;
      // Factor in the number of Upper case letters
      strength += Math.min(3, value.length - value.replace(/[A-Z]/g,"").length) * 10;

      // Normalize between 0 and 100
      return Math.max(0, Math.min(100, strength));
    }
  });
})(jQuery);