How to validate Password Field in android?

前端 未结 8 762
野的像风
野的像风 2020-12-29 10:13

Hi I am very new for android and in my app I have Validations for Change password page.

That means the Password must contain minimum 8 characters at least 1 Alphabet

8条回答
  •  盖世英雄少女心
    2020-12-29 10:47

    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");
        }
    

提交回复
热议问题