How to validate Password Field in android?

public static boolean isValidPassword(String s) {
            Pattern PASSWORD_PATTERN
                    = Pattern.compile(
                    "[a-zA-Z0-9\\!\\@\\#\\$]{8,24}");

            return !TextUtils.isEmpty(s) && PASSWORD_PATTERN.matcher(s).matches();
        }

to use it,

if(isValidPassword(password)){ //password valid}

You can set whatever symbol that you allowed here


try following Code

 //*****************************************************************
public static boolean isValidPassword(final String password) {

    Pattern pattern;
    Matcher matcher;
    final String PASSWORD_PATTERN = "^(?=.*[0-9])(?=.*[A-Z])(?=.*[@#$%^&+=!])(?=\\S+$).{4,}$";
    pattern = Pattern.compile(PASSWORD_PATTERN);
    matcher = pattern.matcher(password);

    return matcher.matches();

}

And change your code to this

   if(newPassword.getText().toString().length()<8 &&!isValidPassword(newPassword.getText().toString())){
        System.out.println("Not Valid");
      }else{
       System.out.println("Valid");
    }

Try this it works

   public static boolean isPasswordValidMethod(final String password) {

    Pattern pattern;
    Matcher matcher;
    final String PASSWORD_PATTERN = "^(?=.*[A-Za-z])(?=.*\\\\d)(?=.*[$@$!%*#?&])[A-Za-z\\\\d$@$!%*#?&]{8,}$""
    pattern = Pattern.compile(PASSWORD_PATTERN);
    matcher = pattern.matcher(password);

    return matcher.matches();

}

public static boolean passwordCharValidation(String passwordEd) {
    String PASSWORD_PATTERN = "^(?=.*[A-Z])(?=.*[@_.]).*$";
    Pattern pattern = Pattern.compile(PASSWORD_PATTERN);
    Matcher matcher = pattern.matcher(passwordEd);
    if (!passwordEd.matches(".*\\d.*") || !matcher.matches()) {
        return true;
    }
    return false;
}

Tags:

Android