var wrjs = wrjs || {}; wrjs.pStrength = (function () { var _display = ''; // mesage to display var _nonNumericsLength = 3; // number of numeric characters that are needed. var _alphaSpecial = 6; // number if alpha and special characters that are needed. var _pwdNumerics = 0; // special characters. var _pwdNonNumerics = 0; // alpha characters var _pwdSpecials = 0; // special characters. var _parseString = function(xx) { var s = ''; xx.forEach(function(x) { s = s + x; }); return s; } var _ProcessPWD = function(t) { t = typeof t == 'object' ? t[0] : t; //var numberOfNumerics = this.value var numericPattern = /[0-9]/g; // Numeric regex pattern var alphaChars = /[a-zA-Z]/g; // Alpha regex pattern var specialChars = /\W/g; // special regex pattern. // searches for the numerics _pwdNumerics = t.value.match(numericPattern) ? _parseString(t.value.match(numericPattern)) : ''; // searches for the alpha characters. _pwdNonNumerics = t.value.match(alphaChars) ? _parseString(t.value.match(alphaChars)) : ''; // searches for the special characters. _pwdSpecials = t.value.match(specialChars) ? _parseString(t.value.match(specialChars)) : ''; // message to display _display = 'Numerics: ' + _pwdNumerics + '\n\r' + 'NonNumerics: ' + _pwdNonNumerics + '\n\r' + 'Specials: ' + _pwdSpecials; // call password strength function for UI display. _pwdStrength(); }; var _MixedCase = function() { var uCasePattern = /[A-Z]/g; var lCasePattern = /[a-z]/g; var countUC = _pwdNonNumerics.match(uCasePattern) ? _pwdNonNumerics.match(uCasePattern) : ''; var countLC = _pwdNonNumerics.match(lCasePattern) ? _pwdNonNumerics.match(lCasePattern) : ''; var mCase = false; if (countUC.length > 0 && countLC.length> 0) { mCase = true; } return mCase; }; var _changeClass = function(weightings) { if (weightings >= 5) { weightings = 5; }; var display = [ ls.text_invalid, ls.text_weak, ls.text_normal, ls.text_strong, ls.text_verystrong ]; var strength = $('#strength') var el = $('span', strength); var field = $('input', strength.parent()) // DONT SHOW STRENGTH RESULT WHEN ZERO CHARACTERS HAVE BEEN ENTERED if(field.val().length == 0) { el.removeClass().text(''); } else { el.removeClass().addClass('password_strength_' + weightings).text(display[weightings - 1]); }; }; var _pwdStrength = function() { var countnonNumbers = _pwdNonNumerics.length + _pwdSpecials.length; var weightings = 1; var pointmixedCase = 0; var pointSpecialChar = 0; var pointgreaterThan9 = 0; // basic check: 3 numbers, 6 alpha / special (combined) if (countnonNumbers >= 6 && _pwdNumerics.length >= _nonNumericsLength) { weightings = 2; // setting the minimum value. // Noting that the mixed case is there. if (_MixedCase()) { pointmixedCase = 1; } // recording special characters (maximum 2 points) pointSpecialChar = _pwdSpecials.length > 2 ? 2 : _pwdSpecials.length; if (countnonNumbers > 6) { pointgreaterThan9 = 1; } } var score = weightings + pointmixedCase + pointSpecialChar + pointgreaterThan9; _changeClass(score); }; // registers the keyUp on the password textbox. $.fn.RegisterChecker = function(){ // registers the required events and binds them to the text box. var t = this; $(this).on('keydown input change paste blur', function(e) { // DELAY FOR ONE MILLISECOND TO ALLOW THE INPUT VALUE TO BE POPULATED setTimeout(function() { _ProcessPWD(t); }); }).trigger('change'); }; var _getInfo = function(){ console.log(_display); }; return { getInfo: _getInfo } })(); // *********************************************************** // THIS WILL ALL GO INTO THE PAGE / OUTSIDE OF THIS JS FILE // *********************************************************** $(document).ready(function() { // calls the registration method. $('#password').RegisterChecker(); $('#NewPassword').RegisterChecker(); });