Skip to content

Instantly share code, notes, and snippets.

@kaleem-elahi
Created October 29, 2020 16:52
Show Gist options
  • Save kaleem-elahi/70adb998145b59e3570ad7c7339d2da9 to your computer and use it in GitHub Desktop.
Save kaleem-elahi/70adb998145b59e3570ad7c7339d2da9 to your computer and use it in GitHub Desktop.
Password strength check (javascript)
export function passwordStrength(value) {
let text;
let strengthClass;
if (
value.length >= 8 &&
/[A-Z]/.test(value) &&
/[a-z]/.test(value) &&
/[0-9]/.test(value) &&
/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/.test(value)
) {
text = 'Strong';
strengthClass = 'strength-green';
} else if (
value.length >= 5 &&
value.length < 8 &&
/[A-Z]/.test(value) &&
/[a-z]/.test(value) &&
/[0-9]/.test(value) &&
/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/.test(value)
) {
text = 'Could be cracked';
strengthClass = 'strength-yellow';
} else if (
value.length >= 5 &&
/[A-Z]/.test(value) &&
/[a-z]/.test(value) &&
/[0-9]/.test(value) &&
!/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/.test(value)
) {
text = 'Could be cracked';
strengthClass = 'strength-yellow';
} else if (
value.length >= 5 &&
/[A-Z]/.test(value) &&
/[a-z]/.test(value) &&
!/[0-9]/.test(value) &&
/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/.test(value)
) {
text = 'Could be cracked';
strengthClass = 'strength-yellow';
} else if (
value.length >= 5 &&
/[A-Z]/.test(value) &&
!/[a-z]/.test(value) &&
/[0-9]/.test(value) &&
/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/.test(value)
) {
text = 'Could be cracked';
strengthClass = 'strength-yellow';
} else if (
value.length >= 5 &&
!/[A-Z]/.test(value) &&
/[a-z]/.test(value) &&
/[0-9]/.test(value) &&
/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/.test(value)
) {
text = 'Could be cracked';
strengthClass = 'strength-yellow';
} else if (value.length >= 5 && value.length < 8) {
text = 'Could be cracked';
strengthClass = 'strength-yellow';
} else if (value.length >= 1 && value.length < 5) {
text = 'Weak';
strengthClass = 'strength-red';
} else if (
value.length >= 8 &&
(!/[A-Z]/.test(value) ||
!/[a-z]/.test(value) ||
!/[0-9]/.test(value) ||
!/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/.test(value))
) {
text = 'Could be cracked';
strengthClass = 'strength-yellow';
}
if (/[ `~^&*_=;':"|,<>/]/.test(value)) {
text = '';
}
return { text, strengthClass };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment