-2

I have seen this question related to javascript but it didn't work for me in android. I want to validate my password which must contain a minimum of 8 in length including characters(Upper or Lower), at least one number? (It should not contain any special symbols)

I have tried this it is working fine with only lower case but not taking Upper case letters.

private boolean isPasswordContainsNumericAndChars(String password) {
        /*^               # start-of-string
        (?=.*\d)          # 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 you can replace with your special characters
        (?=\\S+$)         # no whitespace allowed in the entire string
        .{8,}             # anything, at least eight places though
        $                 # end-of-string*/
        final String PASSWORD_PATTERN = "(?=.*\\d)(?=.*[a-z]).{8,}";
        Pattern pattern = Pattern.compile(PASSWORD_PATTERN);
        Matcher matcher = pattern.matcher(password);
        return matcher.matches();
    }

As he mentioned in the duplicate question which is not working in android.

Minimum eight characters, at least one letter and one number:

 "^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$"
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138

1 Answers1

3

Finally this "(?=.*\\d)(?=.*[a-zA-Z]).{8,}" regex worked.

public static boolean isPasswordContainsNumericAndCharsWithMin8InLength(String password) {
        /*^               # start-of-string
        (?=.*\d)          # 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 you can replace with your special characters
        (?=\\S+$)         # no whitespace allowed in the entire string
        .{8,}             # anything, at least eight places though
        $                 # end-of-string*/
        final String PASSWORD_PATTERN = "(?=.*\\d)(?=.*[a-zA-Z]).{8,}";
        Pattern pattern = Pattern.compile(PASSWORD_PATTERN);
        Matcher matcher = pattern.matcher(password);
        return matcher.matches();
    }
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138