Skip to content

Instantly share code, notes, and snippets.

@sergical
Created December 1, 2016 16:59
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save sergical/389f261b4da04a589fffa0f0f2dd25c2 to your computer and use it in GitHub Desktop.
function isPasswordValid(input) {
if (!hasUppercase(input)) {
console.log('Password needs a capital letter');
}
if (!hasLowercase(input)) {
console.log('Password needs a lowercase letter');
}
if (!isLongEnough(input)) {
console.log('Password needs to be longer');
}
if (!hasSpecialCharacter(input)) {
console.log('Password needs special character');
}
if (hasUppercase(input) && hasLowercase(input) && isLongEnough(input) && hasSpecialCharacter(input)) {
console.log('Password is valid');
}
}
function hasUppercase(input) {
for (var i = 0; i < input.length; i++) {
if (input[i] === input[i].toUpperCase() && input[i] !== input[i].toLowerCase()) {
return true;
}
}
}
function hasLowercase(input) {
for (var i = 0; i < input.length; i++) {
if (input[i] === input[i].toLowerCase() && input[i] !== input[i].toUpperCase()) {
return true;
}
}
}
function isLongEnough(input) {
if (input.length >= 8) {
return true;
}
}
function hasSpecialCharacter(input) {
var specialCharacters = ['!', '@', '#', '$', '%', '^', '&', '*'];
for (var i = 0; i < input.length; i++) {
for (var j = 0; j < specialCharacters.length; j++) {
if (input[i] === specialCharacters[j]) {
return true;
}
}
}
}
isPasswordValid('tylermith!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment