Skip to content

Instantly share code, notes, and snippets.

@farishkash
Created August 10, 2017 03:28
Show Gist options
  • Save farishkash/aa56198a5db0905da93e4b708ebd26eb to your computer and use it in GitHub Desktop.
Save farishkash/aa56198a5db0905da93e4b708ebd26eb to your computer and use it in GitHub Desktop.
function isLetter(str) {
return str.length === 1 && str.match(/[a-z]/i);
}
function hasUppercase(input) {
for (var i = 0; i < input.length; i++) {
if (isLetter(input[i]) == input[i].toUpperCase()) {
return true;
}
}
return false;
}
function hasLowercase(input) {
for (var i = 0; i < input.length; i++) {
if (isLetter(input[i]) == input[i].toLowerCase()) {
return true;
}
}
return false;
}
function isLongEnough(input) {
if (input.length >= 8) {
return true;
} else {
return false;
}
}
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;
}
}
}
}
function isPasswordValid(input) {
if (hasUppercase(input) === true && hasLowercase(input) === true && isLongEnough(input) === true && hasSpecialCharacter(input) === true) {
console.log('The password is valid.');
} else if (!hasUppercase(input)) {
console.log('The password needs a capital letter.');
} else if (!hasLowercase(input)) {
console.log('The password needs a lower case letter.');
}
if (!isLongEnough(input)) {
console.log('The password needs at least 8 letters.');
}
if (!hasSpecialCharacter(input)) {
console.log('The password should have a special character');
}
}
isPasswordValid('pPpppppppppp@');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment