1

I have four checkboxes which will be checked if condition satisfies in text change of edit text. One check box for uppercase, one for length, one for lower case and one for symbol. once user starts to type one of these or all or all possible one needs to be checked.

I have tried :

 mBinding.cetPassword.getEditText().addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {


                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                    if (validateUpperCase(mBinding.cetPassword.getEditText().getText().toString().trim())) {
                        mBinding.chkUpperCase.setChecked(true);
                    }else {
                        mBinding.chkUpperCase.setChecked(false);
                    }

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });

  public boolean validateUpperCase(String name) {
        Pattern ps = Pattern.compile("([A-Z]*)");
        Matcher ms = ps.matcher(name);
        return ms.matches();
    }

It does not works perfectly. on adding second upper case checkbox gets unchecked.

Please suggest! Thank you!

Annie
  • 2,397
  • 3
  • 18
  • 26

3 Answers3

3

don't use if-else used just if like below,

 if (validateUpperCase(mBinding.cetPassword.getEditText().getText().toString().trim())) 
      {    
          mBinding.chkUpperCase.setChecked(true);
      }
if (condition)   {
      mBinding.chkUpperCase.setChecked(false);
         }
Mayur Dabhi
  • 3,607
  • 2
  • 14
  • 25
2

you can check password using this methods:

private static boolean hasLength(CharSequence data) {
return String.valueOf(data).length() >= 8;
}

private static boolean hasSymbol(CharSequence data) {
String password = String.valueOf(data);
return !password.matches("[A-Za-z0-9 ]*");
}

private static boolean hasUpperCase(CharSequence data) {
String password = String.valueOf(data);
return !password.equals(password.toLowerCase());
}

private static boolean hasLowerCase(CharSequence data) {
String password = String.valueOf(data);
return !password.equals(password.toUpperCase());
}

then addTextChangedListener will be like this:

pass_word.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) 
{
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(pass_word.getText().toString().length() > 0){

                if(hasLength(pass_word.getText().toString())){
                    check_length.setChecked(true);
                }else{
                    check_length.setChecked(false);
                }

                if(hasSymbol(pass_word.getText().toString())){
                    check_symbol.setChecked(true);
                }else{
                    check_symbol.setChecked(false);
                }

                if(hasUpperCase(pass_word.getText().toString())){
                    check_upper.setChecked(true);
                }else{
                    check_upper.setChecked(false);
                }

                if(hasLowerCase(pass_word.getText().toString())){
                    check_lower.setChecked(true);
                }else{
                    check_lower.setChecked(false);
                }
            }else{
                check_lower.setChecked(false);
                check_upper.setChecked(false);
                check_length.setChecked(false);
                check_symbol.setChecked(false);
            }
        }
        @Override
        public void afterTextChanged(Editable s) {

        }
    });
Abdomns
  • 428
  • 3
  • 8
  • Hey thanks! your code perfectly works for me. i have one querie regarding hasSymbol method i want to check only some special characters in that. some specific characters. can you suggest for that? – Annie Nov 27 '18 at 10:31
  • if you want check only some special characters in hasSymbol, you can try to use: private static boolean hasLength(CharSequence data) { boolean check_some = data.contains("&") || data.contains("*"); //add what you want. return check_some; } – Abdomns Nov 27 '18 at 10:40
  • Yes it worked! Thank you. One more question i want to ask off topic to this one. i want the check box with the custom drawable and font color. i have achieved custom drawable, but when it is unchecked color should be different and when selected color should be different. any tutorial or link for this? – Annie Nov 27 '18 at 11:03
  • 1
    please check here https://stackoverflow.com/questions/5854047/how-to-change-the-color-of-a-checkbox or this answer https://stackoverflow.com/a/17647520/10408282 – Abdomns Nov 27 '18 at 11:13
  • If you use the Pattern class, it requires that I use two backslashes "\\" before it will accept a hyphen. This is 2021 though, so this may have changed. Online java regex testers only use one backslash as well. – FoxDonut May 19 '21 at 04:43
2

You need to use regex expression. Code Here:

editText.addTextChangedListener(new TextWatcher() {
  @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {

  }

  @Override public void onTextChanged(CharSequence s, int start, int before, int count) {
    String lowerCase = "(.*[a-z].*)";
    String upperCase = "(.*[A-Z].*)";
    String digit = "(.*\\d.*)";
    String symbol = "(.*[:?!@#$%^&*()].*)";

    if(Pattern.matches(lowerCase,s.toString())){
      chkLowercase.setChecked(true);
    }else{
      chkLowercase.setChecked(false);
    }
    if(Pattern.matches(upperCase,s.toString())){
      chkUppercase.setChecked(true);
    }else{
      chkUppercase.setChecked(false);
    }
    if(Pattern.matches(digit,s.toString())){
      chkDigit.setChecked(true);
    }else{
      chkDigit.setChecked(false);
    }
    if(Pattern.matches(symbol,s.toString())){
      chkSymbol.setChecked(true);
    }else{
      chkSymbol.setChecked(false);
    }
  }

  @Override public void afterTextChanged(Editable s) {

  }
});
Chan Myae Aung
  • 547
  • 3
  • 15