Skip to content

Instantly share code, notes, and snippets.

@christopherhill
Last active December 19, 2015 08:49
Show Gist options
  • Save christopherhill/5928546 to your computer and use it in GitHub Desktop.
Save christopherhill/5928546 to your computer and use it in GitHub Desktop.
Password strength and requirements validation.
var password = {
v: "",
update: function() {
this.v = $("#pass").val();
}
}
var criteria = {
update: function() {
password.update();
},
hasLowerCase: {
regex: /[a-z]/,
score: 1,
result: function() {
if (password.v.match(this.regex)) {
return true;
} else {
return false;
}
},
show: function() {
if (this.result() === true) {
$("#lower").removeClass("cinvalid").addClass("cvalid");
} else {
$("#lower").removeClass("cvalid").addClass("cinvalid");
}
return false;
}
},
hasUpperCase: {
regex: /[A-Z]/,
score: 1,
result: function() {
if (password.v.match(this.regex)) {
return true;
} else {
return false;
}
},
show: function() {
if (this.result() === true) {
$("#upper").removeClass("cinvalid").addClass("cvalid");
} else {
$("#upper").removeClass("cvalid").addClass("cinvalid");
}
return false;
}
},
hasNumber: {
regex: /\d+/,
score: 1,
result: function() {
if (password.v.match(this.regex)) {
return true;
} else {
return false;
}
},
show: function() {
if (this.result() === true) {
$("#number").removeClass("cinvalid").addClass("cvalid");
} else {
$("#number").removeClass("cvalid").addClass("cinvalid");
}
return false;
}
},
hasSymbol: {
regex: /[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/,
score: 1,
result: function() {
if (password.v.match(this.regex)) {
return true;
} else {
return false;
}
},
show: function() {
if (this.result() === true) {
$("#symbol").removeClass("cinvalid").addClass("cvalid");
} else {
$("#symbol").removeClass("cvalid").addClass("cinvalid");
};
return false;
}
},
getLength: function() {
return password.v.length;
},
hasLength: {
requirement: 5,
score: 1,
result: function() {
if (criteria.getLength() >= this.requirement) {
return true;
} else {
return false;
}
},
show: function() {
if (this.result() === true) {
$("#length").removeClass("cinvalid").addClass("cvalid");
} else {
$("#length").removeClass("cvalid").addClass("cinvalid");
}
return false;
}
},
meetsAll: function() {
password.update();
if (this.hasLowerCase.result() && this.hasUpperCase.result() && this.hasNumber.result() && this.hasSymbol.result() && this.hasLength.result()) {
return true;
} else {
return false;
}
},
showAll: function() {
this.hasLowerCase.show();
this.hasUpperCase.show();
this.hasNumber.show();
this.hasSymbol.show();
this.hasLength.show();
return false;
}
};
var score = {
value: 0,
clear: function() {
this.value = 0;
return false;
},
limit: function() { // limit strength if password is only 5, 8 chars respectively
var clen = criteria.getLength();
var cmax = 0;
if (clen == 0) {
cmax = 0;
} else if (clen >= 1 && clen < 5) {
cmax = 1;
} else if (clen >= 5 && clen < 8) {
cmax = 2;
} else if (clen >= 8 && clen < 12) {
cmax = 3;
} else if (clen >= 12) {
cmax = 4;
}
this.value = Math.min(cmax, this.value);
return false;
},
update: function() {
password.update();
this.clear();
if (criteria.hasLowerCase.result()) {
this.value += criteria.hasLowerCase.score;
}
if (criteria.hasUpperCase.result()) {
this.value += criteria.hasUpperCase.score;
}
if (criteria.hasNumber.result()) {
this.value += criteria.hasNumber.score;
}
if (criteria.hasSymbol.result()) {
this.value += criteria.hasSymbol.score;
}
this.limit();
return false;
},
descriptions: ["Very Weak", "Weak", "Medium", "Strong", "Very Strong"],
// colors: ["#970a2d", "#c44260", "#9d9c9e", "#4d6849", "#84cc7a"],
// colors: ["#6d2b3b", "#961535", "#b10c37", "#db1b4a", "#ff3064"],
colors: ["#d7f9bb", "#c5ff9b", "#b4fc79", "#a2ff56", "#8dff30"],
show: function() {
this.update();
if (this.value <= this.descriptions.length && this.value > 0) {
$("#pstrength").text(this.descriptions[this.value]).fadeIn();
} else {
$("#pstrength").empty();
}
this.meter();
return false;
},
meter: function() {
// get width of div
var pmw = 80;
var pme = pmw / 5;
var curwidth = pme * this.value;
curwidth = curwidth.toString() + "px";
$("#pmeter").css("background", this.colors[this.value])
// .css("width", curwidth);
.animate({ width: curwidth }, 120);
return false;
}
}
function removeSubmit() {
$("#passSubmit").attr("disabled", "disabled").addClass("greyedout");
return false;
}
function showSubmit() {
$("#passSubmit").removeAttr("disabled").removeClass("greyedout");
return false;
}
function validatePassword(pass) {
password.update();
criteria.showAll(); // show the results of the criteria in the div
score.show(); // show the score in the div
var returnval = false;
// if (criteria.meetsAll() === true) {
if (criteria.hasLength.result() === true) {
returnval = true;
}
return returnval;
}
function validateConfirmPassword(password) {
// check to see if equal to value of the first password
var currpass = $("#pass").val();
if ((currpass === $("#confirmPass").val()) && (criteria.hasLength.result() === true)) { /* (criteria.meetsAll() === true)) { */
showSubmit();
return true;
} else {
removeSubmit();
return false;
}
}
function writeMsg(msgtext, msgtype, timeoutval) {
if (typeof(msgtype) === undefined) {
msgtype = "Error";
}
if (typeof(timeoutval) === undefined) {
timeoutval = 5000;
}
if (msgtype === "Error") {
$("#messageBox").css("background-color", "#EC3F41");
} else if (msgtype === "Success") {
$("#messageBox").css("background-color", "#3A7D34");
}
$("#messageBox").fadeOut('slow');
if (typeof(msgtext) == 'undefined') {
return false;
} else {
$("#messageBox").text(msgtext);
$("#messageBox").fadeIn('slow');
setTimeout(function() {
$("#messageBox").fadeOut('slow'); }
, timeoutval);
return true;
}
}
function initAll() {
$("#pass").val("");
$("#confirmPass").val("");
$("#pconfirm").empty();
criteria.update();
criteria.showAll();
score.clear();
score.show();
removeSubmit();
$('#resetPasswordForm :input:visible:enabled:first').focus();
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment