Skip to content

Instantly share code, notes, and snippets.

@frayhan32
Last active December 23, 2015 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frayhan32/6589385 to your computer and use it in GitHub Desktop.
Save frayhan32/6589385 to your computer and use it in GitHub Desktop.
Strength password
/**
* Strength Password Class
*
* Build with full of the love
*
* Faris Rayhan
*
* Copyright 2014
*/
// Valid password must have at least
// one number , one big letter , one normal character and at least 8 character with maximum 32
function strengthPassword(val) {
//Our value
this.val = val;
//Create breaker for criteria
//Defaultly no normal char
this.isNormalChar = false
//And also with big letter
this.isBigLetter = false;
//And with number too
this.isNumber = false;
//Our valid function
this.doValidation = function () {
//Lets break some of our validation if can
if (/\d/g.exec(this.val) !== null) { //if digit found
this.isNumber = true; //isNumber is broken set to true
}
if (/[A-Z]/g.exec(this.val) !== null) { //if big letter found
this.isBigLetter = true; //isBigLetter is broken set to true
}
if (/[a-z]/g.exec(this.val) !== null) { //if small letter found
this.isNormalChar = true; //isNormalChar is broken set to true
}
//And this to warn the user if one of our breaker still false
if (this.isBigLetter === false) { //we do not find big letter
return 'At least one big letter';
}
else if (this.isNumber === false) { //we do not find number
return "At least one number";
}
else if (this.isNormalChar === false) { //we do not find normal charatcter
return "At least one normal char";
}
else if (this.val.length < 8 || this.val.length > 32) { //entered password less than 8 or more than 32
return "Minimum 8 but no more than 32 in length";
}
else { //everything is okay
return "Okay you have fill all criteria as well";
}
}
}
//Test him
var a = new strengthPassword('a12');
console.log(a.doValidation()); //At least one big letter
var b = new strengthPassword('aA');
console.log(b.doValidation()); //At least one number
var c = new strengthPassword('1A');
console.log(c.doValidation()); //At least one normal char
var d = new strengthPassword('A1a');
console.log(d.doValidation()); //"Minimum 8 but no more than 32 in length
var e = new strengthPassword('123Aaaaa');
console.log(e.doValidation()); //Okay you have fulfill all criteria
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment