1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| private static final Set<Character> CHAR_SET = new HashSet<>(Arrays.asList ('~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+'));
public boolean isValidPassword(String password){ boolean isLengthNotLess8, isContainDigital, isContainLowerLetter, isContainUpperLetter, isContainSpecialChar; int digitalCount = 0, lowerLetterCount = 0, upperLetterCount = 0, specialCharCount = 0; isLengthNotLess8 = password.length() >= 8; for (int i = 0; i < password.length(); i++) { char cha = password.charAt(i); if (cha <= '9' && cha >= '0') { digitalCount++; } else if (cha <= 'z' && cha >= 'a') { lowerLetterCount++; } else if (cha <= 'Z' && cha >= 'A') { upperLetterCount++; } else if (CHAR_SET.contains(cha)) { specialCharCount++; } } isContainDigital = digitalCount >= 1; isContainLowerLetter = lowerLetterCount >= 1; isContainUpperLetter = upperLetterCount >= 1; isContainSpecialChar = specialCharCount >= 1;
return isLengthNotLess8 && isContainDigital && isContainLowerLetter && isContainUpperLetter && isContainSpecialChar; }
|