Dotclear

Changeset 3689:f0f98c0c6124 for admin/js


Ignore:
Timestamp:
01/27/18 13:22:08 (8 years ago)
Author:
franck <carnet.franck.paul@…>
Branch:
default
Message:

Better way (but not the best) to evaluate password strength

File:
1 edited

Legend:

Unmodified
Added
Removed
  • admin/js/jquery/jquery.pwstrength.js

    r1371 r3689  
    1 /** 
    2  * jquery.pwstrength http://matoilic.github.com/jquery.pwstrength 
    3  * 
    4  * @version v0.1.1 
    5  * @author Mato Ilic <info@matoilic.ch> 
    6  * @copyright 2013 Mato Ilic 
    7  * 
    8  * Dual licensed under the MIT and GPL licenses: 
    9  * http://www.opensource.org/licenses/mit-license.php 
    10  * http://www.gnu.org/licenses/gpl.html 
    11  */ 
    12 ;(function($) { 
    13     $.pwstrength = function(password) { 
    14         var score = 0, length = password.length, upperCase, lowerCase, digits, nonAlpha; 
    15          
    16         if(length < 5) score += 0; 
    17         else if(length < 8) score += 5; 
    18         else if(length < 16) score += 10; 
    19         else score += 15; 
    20          
    21         lowerCase = password.match(/[a-z]/g); 
    22         if(lowerCase) score += 1; 
    23          
    24         upperCase = password.match(/[A-Z]/g); 
    25         if(upperCase) score += 5; 
    26          
    27         if(upperCase && lowerCase) score += 2; 
    28          
    29         digits = password.match(/\d/g); 
    30         if(digits && digits.length > 1) score += 5; 
    31          
    32         nonAlpha = password.match(/\W/g) 
    33         if(nonAlpha) score += (nonAlpha.length > 1) ? 15 : 10; 
    34          
    35         if(upperCase && lowerCase && digits && nonAlpha) score += 15; 
     1(function($) { 
     2  $.pwstrength = function(password) { 
     3    var score = 0; 
     4    var symbols = '[!,@,#,$,%,^,&,*,?,_,~]'; 
     5    // Regexp 
     6    var check = new RegExp('(' + symbols + ')'); 
     7    var doublecheck = new RegExp('(' + '.*' + symbols + '.*' + symbols + ')'); 
     8    // password length 
     9    score += password.length * 4; 
     10    score += checkRepetition(1, password).length - password.length; 
     11    score += checkRepetition(2, password).length - password.length; 
     12    score += checkRepetition(3, password).length - password.length; 
     13    score += checkRepetition(4, password).length - password.length; 
     14    // password has 3 numbers 
     15    if (password.match(/(.*[0-9].*[0-9].*[0-9])/)) { 
     16      score += 5; 
     17    } 
     18    // password has at least 2 symbols 
     19    if (password.match(doublecheck)) { 
     20      score += 5; 
     21    } 
     22    // password has Upper and Lower chars 
     23    if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) { 
     24      score += 10; 
     25    } 
     26    // password has number and chars 
     27    if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)) { 
     28      score += 15; 
     29    } 
     30    // password has number and symbol 
     31    if (password.match(check) && password.match(/([0-9])/)) { 
     32      score += 15; 
     33    } 
     34    // password has char and symbol 
     35    if (password.match(check) && password.match(/([a-zA-Z])/)) { 
     36      score += 15; 
     37    } 
     38    // password is just numbers or chars 
     39    if (password.match(/^\w+$/) || password.match(/^\d+$/)) { 
     40      score -= 10; 
     41    } 
     42    return Math.floor(Math.max(Math.min(score, 99), 0) / 20); 
     43  }; 
    3644 
    37         if(password.match(/\s/)) score += 10; 
     45  function checkRepetition(rLen, str) { 
     46    var res = "", 
     47      repeated = false; 
     48    for (var i = 0; i < str.length; i++) { 
     49      repeated = true; 
     50      for (var j = 0; j < rLen && (j + i + rLen) < str.length; j++) { 
     51        repeated = repeated && (str.charAt(j + i) === str.charAt(j + i + rLen)); 
     52      } 
     53      if (j < rLen) { 
     54        repeated = false; 
     55      } 
     56      if (repeated) { 
     57        i += rLen - 1; 
     58        repeated = false; 
     59      } else { 
     60        res += str.charAt(i); 
     61      } 
     62    } 
     63    return res; 
     64  } 
    3865 
    39         if(score < 15) return 0; 
    40         if(score < 20) return 1; 
    41         if(score < 35) return 2; 
    42         if(score < 50) return 3; 
    43         return 4; 
    44     }; 
    45      
    46     function updateIndicator(event) { 
    47         var strength = $.pwstrength($(this).val()), options = event.data, klass; 
    48         klass = options.classes[strength]; 
    49          
    50         options.indicator.removeClass(options.indicator.data('pwclass')); 
    51         options.indicator.data('pwclass', klass); 
    52         options.indicator.addClass(klass); 
    53         options.indicator.find(options.label).html(options.texts[strength]); 
    54     } 
    55      
    56     $.fn.pwstrength = function(options) { 
    57         var options = $.extend({ 
    58             label: '.label', 
    59             classes: ['pw-very-weak', 'pw-weak', 'pw-mediocre', 'pw-strong', 'pw-very-strong'], 
    60             texts: ['very weak', 'weak', 'mediocre', 'strong', 'very strong'] 
    61         }, options || {}); 
    62         options.indicator = $('#' + this.data('indicator')); 
    63          
    64         return this.keyup(options, updateIndicator); 
    65     }; 
     66  function updateIndicator(event) { 
     67    var strength = $.pwstrength($(this).val()), 
     68      options = event.data, 
     69      klass; 
     70    klass = options.classes[strength]; 
     71    options.indicator.removeClass(options.indicator.data('pwclass')); 
     72    options.indicator.data('pwclass', klass); 
     73    options.indicator.addClass(klass); 
     74    options.indicator.find(options.label).html(options.texts[strength]); 
     75  } 
     76  $.fn.pwstrength = function(options) { 
     77    var options = $.extend({ 
     78      label: '.label', 
     79      classes: ['pw-very-weak', 'pw-weak', 'pw-mediocre', 'pw-strong', 'pw-very-strong'], 
     80      texts: ['very weak', 'weak', 'mediocre', 'strong', 'very strong'] 
     81    }, options || {}); 
     82    options.indicator = $('#' + this.data('indicator')); 
     83    return this.keyup(options, updateIndicator); 
     84  }; 
    6685})(jQuery); 
Note: See TracChangeset for help on using the changeset viewer.

Sites map