Created
May 31, 2014 19:27
-
-
Save philbritton/4681f7600192117a685f to your computer and use it in GitHub Desktop.
strong password validation form, auto update as you type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var atLeastOneLetter = /.*[a-zA-Z]{1,}.*/; | |
var atLeastOneNumberOrSpecialChar = /.*[0-9\\\(\)\[\]\{\}\-\+\=\,\?\"_`:@'#*$;^!|~/\.]{1,}.*/; | |
var atLeastOneSpace = /.*[ ]{1,}.*/; | |
var specialChars = /(?=.*[><&%]).{1,}/; | |
var imgPath = 'images/'; | |
var check_IMG = imgPath + 'check16.gif'; | |
var wrong_IMG = imgPath + 'wrong16.gif'; | |
var clear_IMG = imgPath + 'clear16.gif'; | |
var passwordTipPopupId = 'passwordTipPopup'; | |
var pwdTipImagesId = 'pwdTip'; | |
var newPasswordFieldId = 'changePassword_newPasswordId'; | |
function checkPassword() { | |
toggleOnPopup(passwordTipPopupId); | |
validatePassword('true',""); | |
} | |
function onBlurNewPassword() { | |
toggleOffPopup(passwordTipPopupId); | |
} | |
function toggleOffPopup(popupId) { | |
var popup = $('#' + popupId); | |
popup.hide(); | |
} | |
function toggleOnPopup(popupId) { | |
var parent = $('#' + newPasswordFieldId).parent(); | |
var pwPopup = $('#' + passwordTipPopupId); | |
pwPopup.remove(); | |
parent.append(pwPopup); | |
$('#' + passwordTipPopupId).css({display: 'block'}); | |
} | |
function validateChangePassword(invalidPwdMsg,newPwdErrorMsgs,confirmPwdMsg) { | |
var isValidCurrentPwd = validateCurrentPassword(invalidPwdMsg); | |
var isValidPwd = validatePassword('false',newPwdErrorMsgs); | |
var isValildConfirmPassword = valildateConfirmPassword(invalidPwdMsg,confirmPwdMsg); | |
validChangePassword = isValidCurrentPwd && isValidPwd && isValildConfirmPassword; | |
setFocus(isValidCurrentPwd,isValidPwd,isValildConfirmPassword); | |
return validChangePassword; | |
} | |
function setFocus(isValidCurrentPwd,isValidPwd,isValildConfirmPassword){ | |
if(!isValidCurrentPwd){ | |
$w("changePassword.currentPassword").widgetDiv.find('input').focus(); | |
}else if(!isValidPwd){ | |
$w("changePassword.newPassword").widgetDiv.find('input').focus(); | |
}else if(!isValildConfirmPassword){ | |
$w("changePassword.confirmNewPassword").widgetDiv.find('input').focus(); | |
}else{ | |
$w("changePassword.currentPassword").widgetDiv.find('input').focus(); | |
} | |
} | |
function validateCurrentPassword(rqdPwdMsg) { | |
var widget = $w("changePassword.currentPassword"); | |
if(isEmpty(widget.getValue())) { | |
widget.setMessage(rqdPwdMsg); | |
return false; | |
} | |
widget.setMessage(""); | |
return true; | |
} | |
function valildateConfirmPassword(invalidPwdMsg,confirmPwdMsg) { | |
var newPwWidget = $w("changePassword.newPassword"); | |
var confirmPwWidget = $w("changePassword.confirmNewPassword"); | |
var newPassword = newPwWidget.getValue(); | |
var confirmPassword = confirmPwWidget.getValue(); | |
if(isEmpty(confirmPassword) && isEmpty(newPassword)) { | |
newPwWidget.setMessage(invalidPwdMsg); | |
confirmPwWidget.setMessage(invalidPwdMsg); | |
return false; | |
} | |
if(newPassword != confirmPassword) { | |
confirmPwWidget.setMessage(confirmPwdMsg); | |
return false; | |
} | |
return true; | |
} | |
function validatePassword(isSwapImg,newPwdErrorMsgs) { | |
var widget = $w("changePassword.newPassword"); | |
var pwdValue = widget.getValue(); | |
var newPwdErrorMsgArr = newPwdErrorMsgs.split('|'); | |
widget.setMessage(""); | |
if(isEmpty(pwdValue) && isSwapImg == 'false') { | |
widget.setMessage(newPwdErrorMsgArr[0]); | |
return false; | |
} | |
var pwdTipsCnt = $('[name="pwdTip"]').length; | |
var custIdVal = $('[name="changePassword.customerId"]:eq(0)').val(); | |
var result = 0; | |
var isErrorMessageDisplayed = false; | |
var noSpecialCharsValid = !(specialChars.test(pwdValue)); | |
result = result + (noSpecialCharsValid ? 0 : 1); | |
if(result != 0 && isSwapImg == 'false') { | |
widget.setMessage(newPwdErrorMsgArr[1]); | |
return false; | |
} | |
for(var i = 1; i <= pwdTipsCnt; i++) { | |
switch(i) { | |
case 1: var pwdSizeValid = pwdValue.length >= 8 && pwdValue.length <= 35; | |
result = result + (pwdSizeValid ? 0 : 1); | |
if(isSwapImg == 'true') { | |
swapImage(pwdTipImagesId +i,pwdSizeValid); | |
} | |
else if(result != 0 && !isErrorMessageDisplayed) { | |
widget.setMessage(newPwdErrorMsgArr[2]); | |
isErrorMessageDisplayed = true; | |
} | |
break; | |
case 2: var atleastOneAlphaValid = atLeastOneLetter.test(pwdValue); | |
result = result + (atleastOneAlphaValid ? 0 : 1); | |
if(isSwapImg == 'true') { | |
swapImage(pwdTipImagesId +i,atleastOneAlphaValid); | |
} | |
else if(result != 0 && !isErrorMessageDisplayed) { | |
widget.setMessage(newPwdErrorMsgArr[3]); | |
isErrorMessageDisplayed = true; | |
} | |
break; | |
case 3: var atLeastOneNumberOrSpecialCharValid = atLeastOneNumberOrSpecialChar.test(pwdValue); | |
result = result + (atLeastOneNumberOrSpecialCharValid ? 0 : 1); | |
if(isSwapImg == 'true') { | |
swapImage(pwdTipImagesId +i,atLeastOneNumberOrSpecialCharValid); | |
} | |
else if(result != 0 && !isErrorMessageDisplayed) { | |
widget.setMessage(newPwdErrorMsgArr[4]); | |
isErrorMessageDisplayed = true; | |
} | |
break; | |
case 4: var noSpacesValid = !(atLeastOneSpace.test(pwdValue)); | |
result = result + (noSpacesValid ? 0 : 1); | |
if(isSwapImg == 'true') { | |
swapImage(pwdTipImagesId +i,noSpacesValid); | |
} | |
else if(result != 0 && !isErrorMessageDisplayed) { | |
widget.setMessage(newPwdErrorMsgArr[5]); | |
isErrorMessageDisplayed = true; | |
} | |
break; | |
case 5: var noCustIdValid = !(pwdValue.indexOf(custIdVal) >= 0); | |
result = result + (noCustIdValid ? 0 : 1); | |
if(isSwapImg == 'true') { | |
swapImage(pwdTipImagesId +i,noCustIdValid); | |
} | |
else if(result != 0 && !isErrorMessageDisplayed) { | |
widget.setMessage(newPwdErrorMsgArr[6]); | |
isErrorMessageDisplayed = true; | |
} | |
break; | |
case 6: var noRepeatingCharValid = !(testForRepeating(pwdValue)); | |
result = result + (noRepeatingCharValid ? 0 : 1); | |
if(isSwapImg == 'true') { | |
swapImage(pwdTipImagesId +i,noRepeatingCharValid); | |
} | |
else if(result != 0 && !isErrorMessageDisplayed) { | |
widget.setMessage(newPwdErrorMsgArr[7]); | |
isErrorMessageDisplayed = true; | |
} | |
break; | |
default: | |
break; | |
} | |
} | |
return result == 0; | |
} | |
function testForRepeating(x) { | |
var letters = []; | |
for (var i=1; i <= x.length; i++) { | |
letters[i] = x.substring(i-1, i); | |
} | |
var maxRepeat = Math.floor(x.length/2); | |
var repeatingCharactersPattern = new RegExp(/([\w|\W])\1{2,}/); | |
if(repeatingCharactersPattern.test(x.toLowerCase())){ | |
return true; | |
} | |
var num = 0; | |
for (var i=1; i< letters.length; i++) { | |
if(letters[i-1] == letters[i]) { | |
if (num == 0) { | |
num = 2; | |
} else { | |
num++; | |
} | |
if (num > maxRepeat) { | |
return true; | |
} | |
} else { | |
num = 0; | |
} | |
} | |
return false; | |
} | |
function swapImage(myImg, bool) { | |
var myImage = document.getElementById(myImg); | |
if (bool) { | |
document.images[myImg].src = check_IMG; | |
} | |
else { | |
if(document.images[myImg].src.indexOf(check_IMG) >-1 || document.images[myImg].src.indexOf(wrong_IMG) >-1) { | |
document.images[myImg].src = wrong_IMG; | |
} else { | |
document.images[myImg].src = clear_IMG; | |
} | |
} | |
return bool; | |
} | |
function isEmpty(value) { | |
if (value.length == 0) | |
{ | |
return true; | |
} | |
return false; | |
} | |
function updateOnKeyDown(evt) { | |
if (!isObject(evt)) { | |
evt = window.event; | |
} | |
var keyCode = evt.keyCode; | |
if(keyCode === SFXKEYS.ENTER) { | |
buttons.get('updateButton').fireOnClick(); | |
} | |
return true; | |
} | |
function changePasswordOnload() { | |
loadWebtrendsErrorMsg(); | |
$w("changePassword.currentPassword").widgetDiv.find('input').focus(); | |
$w('changePassword.currentPassword').clear(); | |
$w('changePassword.newPassword').clear(); | |
$w('changePassword.confirmNewPassword').clear(); | |
} | |
function loadWebtrendsErrorMsg() { | |
var errorMessageTxt = $('#errorMsgTxtId'); | |
var webTrendsMessageTxt = $('[name="changePassword.webTrendsErrorMsg"]:eq(0)'); | |
if(errorMessageTxt!=null) { | |
var txt = errorMessageTxt.html(); | |
webTrendsMessageTxt.val(txt); | |
} | |
} | |
load(function() { | |
changePasswordOnload(); | |
$('#sfMain input').bind("keydown", updateOnKeyDown); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment