How to validate Password Field in android?

前端 未结 8 700
野的像风
野的像风 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:26

    Simple and sort, just use below method for checking valid password,

    public static boolean isValidPassword(String password) {
        Matcher matcher = Pattern.compile("((?=.*[a-z])(?=.*\\d)(?=.*[A-Z])(?=.*[@#$%!]).{4,20})").matcher(password);
        return matcher.matches();
    }
    

    and you can use this method as below

              if (!isValidPassword(edtPassword.getText().toString())) {
                    errorDialog("Password must contain mix of upper and lower case letters as well as digits and one special charecter(4-20)");
                    }
    
    0 讨论(0)
  • 2020-12-29 10:31

    easy piece of code for email field validation and password validation and check for minimum 8 characters in password field.

      if (isValidEmail(et_regemail.getText().toString())&&etpass1.getText().toString().length()>7){
          if (validatePassword(etpass1.getText().toString())) {
          Toast.makeText(getApplicationContext(),"Go Ahead".....
          }
          else{
    
           Toast.makeText(getApplicationContext(),"InvalidPassword".....
           }
    
    }else{
    
     Toast.makeText(getApplicationContext(),"Invalid Email".....
    }
    
    
    public boolean validatePassword(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();
    }
    
    public final static boolean isValidEmail(CharSequence target) {
        if (target == null)
            return false;
    
        return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
    }
    
    0 讨论(0)
  • 2020-12-29 10:32
    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;
    }
    
    0 讨论(0)
  • 2020-12-29 10:33

    Passwords do not match

    if  (password.getText().toString() != reEnteredPassword.getText().toString()) {
    //not match
    }
    

    For Empty Fields

    if (password.getText().toString().isEmpty() || reEnteredPassword.getText().toString().isEmpty()) {
    //Fieds empty error message
    

    Less than 8 characters

    if ((password.getText().toString().length() < 8) || (reEnteredPassword.getText().toString().length() < 8)) {
    // less than 8 characters error message
    }
    

    Not having special characters

    if (!password.getText().toString().matches( "^(?=.*[0-9])(?=.*[a-z])(?=.*[!@#$%^&*+=?-]).{8,15}$") || !reEnteredPassword.getText().toString().matches( "^(?=.*[0-9])(?=.*[a-z])(?=.*[!@#$%^&*+=?-]).{8,15}$")){
    //not having special characters error message
    }
    
    0 讨论(0)
  • 2020-12-29 10:40

    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();
    
    }
    
    0 讨论(0)
  • 2020-12-29 10:41
    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

    0 讨论(0)
提交回复
热议问题