$(document).ready(function(){ var translations; // Add meter element $('.psm').each(function(){ var relatedInput = $('#'+$(this).attr('rel')); $(this).append( '
' + '
' + '
' + '
' ); var that = this; var minlength = $(that).data('minlength') || 7; $.getJSON(Config.baseUrl + 'translations/passwordstrength', {'minlength' : minlength}, function(data){ translations = data; }); relatedInput.keyup(function(){ // Get vars var meter = $('.psm_' + $(this).attr('name') + ' .pwd_meter div'); var text = $('.psm_' + $(this).attr('name') + ' .pwd_text'); var perc = passwordStrengthPercent($(this).val(), minlength); // Set the accompanying text text.text(passwordStrengthString(perc, translations)); // Minimum 5 perc perc = perc < 5 ? 5 : perc; // Set width meter.css('width', perc+'%'); }); }); }); function passwordStrengthString(perc, translations) { if ( perc > 75 ) { return translations[100]; // very strong } else if ( perc > 50 ) { return translations[75]; // strong } else if ( perc > 25 ) { return translations[50]; // weak } else if ( perc > 0 ) { return translations[25]; // very weak } else if ( perc == 0 ) { return translations[0]; // not longh enough } else if(perc < 0) { return translations['-1']; // no password } } function passwordStrengthPercent(password, minlength){ var score = 0 // empty password if (password.length == 0) { return -1 } // password < 4 if (password.length < minlength) { return 0 } // password length score += password.length * 4 score += ( checkRepetition(1,password).length - password.length ) * 1 score += ( checkRepetition(2,password).length - password.length ) * 1 score += ( checkRepetition(3,password).length - password.length ) * 1 score += ( checkRepetition(4,password).length - password.length ) * 1 // password has 3 numbers if (password.match(/(.*[0-9].*[0-9].*[0-9])/)) score += 10 // password has 2 symbols if (password.match(/(.*[^a-zA-Z0-9].*[^a-zA-Z0-9])/)) score += 10 // password has Upper and Lower chars if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) score += 10 // password has number and chars if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)) score += 15 // // password has number and symbol if (password.match(/([^a-zA-Z0-9])/) && password.match(/([0-9])/)) score += 15 // password has char and symbol if (password.match(/([^a-zA-Z0-9])/) && password.match(/([a-zA-Z])/)) score += 15 // password is just a numbers or chars if (password.match(/^\w+$/) || password.match(/^\d+$/) ) score -= 10 if (score > 100) return 100 return (score) } function checkRepetition(pLen,str) { var res = "", repeat, repeated; for ( i = 0; i < str.length ; i++ ) { repeated = true; for (j = 0; j < pLen && (j + i + pLen) < str.length; j++){ repeated = repeated && (str.charAt(j + i) == str.charAt(j + i + pLen)); } if (j < pLen) { repeated = false; } if (repeated) { i += pLen - 1; repeated = false; } else { res += str.charAt(i); } } return res }