Reputation: 2587
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!
Upvotes: 0
Views: 2871
Reputation: 547
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) {
}
});
Upvotes: 2
Reputation: 3926
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);
}
Upvotes: 2
Reputation: 428
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) {
}
});
Upvotes: 2