How to validate Password Field in android?

前端 未结 8 701
野的像风
野的像风 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:44
      String validPassword = "12345";
                         _Password_String = Password.getText().toString();
                        Matcher matcher = Pattern.compile(validPassword).matcher(_Password_String);
                        if (matcher.matches()) {
                            Log.e("d11", _Password_String);
                            Toast.makeText(getActivity(), "Password Match", Toast.LENGTH_LONG).show();
                            getFragmentManager().popBackStack();
    
    
                        } else {
                            Password.setError("Password");
                            Toast.makeText(getActivity(), "Password not Match", Toast.LENGTH_LONG).show();
                        }
    
    0 讨论(0)
  • 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");
        }
    
    0 讨论(0)
提交回复
热议问题