Created
November 4, 2018 23:25
-
-
Save rcebrian/9ebe20c1f3ceea0b6b1efe5379abd4fe to your computer and use it in GitHub Desktop.
check that a password meets all the requirements
This file contains hidden or 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
/* | |
* validatePassword: check that a password meets all the requirements | |
* @param passwd: a string with the password that you want to check | |
* @return true: meet the requirements | |
* @return false: doesn't meet the requirements | |
*/ | |
public static boolean validatePassword(String passwd) { | |
boolean valid = false; | |
Pattern pattern = Pattern.compile("(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[~`!@#$%^&*()+=\\-\\[\\]:.;,<>?/'\"{}|_])(?=\\S+$).{8,12}"); | |
if (passwd != null) { | |
Matcher mather = pattern.matcher(passwd); | |
if (mather.find()) | |
valid = true; | |
} | |
return valid; | |
} | |
/* - - NOTES - - | |
* (?=.*[0-9]) a digit must occur at least once | |
* (?=.*[a-z]) a lower case letter must occur at least once | |
* (?=.*[A-Z]) an upper case letter must occur at least once | |
* (?=.*[~`!@#$%^&*()+=\\-\\[\\]:.;,<>?/'\"{}|_]) a special character must occur at least once | |
* (?=\\S+$) no whitespace allowed in the entire string | |
* .{8,12} at least 8 chars and 12 max | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment