Created
October 29, 2020 16:52
-
-
Save kaleem-elahi/70adb998145b59e3570ad7c7339d2da9 to your computer and use it in GitHub Desktop.
Password strength check (javascript)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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