$(function () { "user strict"; /** * start password checking logic */ password.init(); }); /** * Hold functions related to client side password validation */ var password = (function () { "use strict"; var minPasswordLength = 6; var _hasLowerCase = /[a-z]/; var _hasUpperCase = /[A-Z]/; var _hasDigit = /\d/; // match special characters except space var _hasSpecial = /(_|[^\w\d ])/; var password = []; var password2 = []; var hasEightCharsListItem, hasUpperCaseListItem, hasLowerCaseListItem, hasSpecialListItem, hasDigitListItem; // Enforces that password2 = password, and display an error message if it does not var mustMatch = function () { if (password.val() !== password2.val()) { // show not matching error password2.addClass('invalid'); return false; } password2.removeClass('invalid'); return true; }; // check validation and adjust classes var checkAndSwitchClasses = function (has, $element) { if (has) { $element.find(".glyphicon").removeClass("glyphicon-remove-circle").removeClass('invalid').addClass('valid').addClass("glyphicon-ok-circle"); return true; } $element.find(".glyphicon").removeClass("glyphicon-ok-circle").removeClass('valid').addClass('invalid').addClass("glyphicon-remove-circle"); return false; }; // Enforces server side password rules on the client for convenience var enforceRules = function () { $('.invalid').removeClass('invalid'); var pw = password.val().toLowerCase(); var hasEight = pw.length >= minPasswordLength; var hasLower = _hasLowerCase.test(password.val()); var hasUpper = _hasUpperCase.test(password.val()); var hasDigit = _hasDigit.test(password.val()); var hasSpecial = _hasSpecial.test(password.val()); checkAndSwitchClasses(hasEight, hasEightCharsListItem); checkAndSwitchClasses(hasLower, hasLowerCaseListItem); checkAndSwitchClasses(hasUpper, hasUpperCaseListItem); checkAndSwitchClasses(hasDigit, hasDigitListItem); checkAndSwitchClasses(hasSpecial, hasSpecialListItem); if (pw.length === 0) $('.invalid').removeClass('invalid'); // don't move forward until the password is actually *good* /* if (!(hasEight && hasLower && hasUpper && hasDigit && hasSpecial)) { return false; }*/ if (mustMatch() && (hasEight && hasLower && hasUpper && hasDigit && hasSpecial)){ $('#submit').addClass('valid'); } else { $('#submit').removeClass('valid'); } }; return { init: function () { // hook all password/password2 fields on a page password = $('#password'); password2 = $('#password-verify'); // hook all req list items hasEightCharsListItem = $('#req-length'); hasUpperCaseListItem = $('#req-upper'); hasLowerCaseListItem = $('#req-lower'); hasSpecialListItem = $('#req-special'); hasDigitListItem = $('#req-digit'); password.keyup(enforceRules); password2.keyup(enforceRules); } }; }());