Skip to content

Instantly share code, notes, and snippets.

@rcebrian
Created November 4, 2018 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcebrian/9ebe20c1f3ceea0b6b1efe5379abd4fe to your computer and use it in GitHub Desktop.
Save rcebrian/9ebe20c1f3ceea0b6b1efe5379abd4fe to your computer and use it in GitHub Desktop.
check that a password meets all the requirements
/*
* 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