I have login page that check for password strength. There is an categories i need to check for regular expressions. How can i check in java? The main problem is that when input as "HELLOWORLDHELLO" or 123456789012 . It still can be accepted. Not going into false.If input is not equal to at least below two categories. It should be return false. Thanks.Below is my password strength. Password must be more than 12 characters and contains at least 2 of categories below:
At least 1 upper case character.
At least 1 lower case character.
At least 1 digit number.
At least 1 special character. (!,$,#,%,etc.)
And my java code is below.
public static final String UPPER_CASE_REGEX = "^(?=.*[A-Z])[A-Za-z\\d@$!%*?&^~#*()`<>,.|+-_{}:;\'\"\\/]{12,}$";
public static final String LOWER_CASE_REGEX = "^(?=.*[a-z])[A-Za-z\\d@$!%*?&^~#*()`<>,.|+-_{}:;\'\"\\/]{12,}$";
public static final String NUMBER_REGEX = "^(?=.*\\d)[A-Za-z\\d@$!%*?&^~#*()`<>,.|+-_{}:;\'\"\\/]{12,}$";
public static final String SPECIAL_CHAR_REGEX = "^(?=.*[@$!%*?&^~#*()`<>,.|+-_{}:;\'\"\\/])[A-Za-z\\d@$!%*?&^~#*()`<>,.|+-{}:;\'\"\\/]{12,}$";
private static boolean checkStrength(String input) {
Pattern upperCasePattern = Pattern.compile(UPPER_CASE_REGEX);
Matcher upperCaseMatcher = upperCasePattern.matcher(input);
int matchCount = 0;
if(upperCaseMatcher.matches()) {
matchCount++;
if(matchCount >= 2) {
return true;
}
}
if(lowerCaseMatcher.matches()) {
matchCount++;
if(matchCount >= 2) {
return true;
}
}
if(numberMatcher.matches()) {
matchCount++;
if(matchCount >= 2) {
return true;
}
}
if(specialCharMatcher.matches()) {
matchCount++;
if(matchCount >= 2) {
return true;
}
}
return false;
}