-1

in android is it possible to restrict edittext in order to reject and show an alert if some words are entered? For example I need to exclude from the edittext input the following words and numerc sequences: "123456", "0000", "administrator", "black" etc. Someone can help me please?

I used the following code to check and show alert when edittext is blank

    if (TextUtils.isEmpty(name)) {
        editTextName.setError("Please enter name");
        editTextName.requestFocus();
        return;
    }

but I don't know how to check if was entered restricted words.

Thanks Andrew

Andrew
  • 3
  • 2
  • Have you tried anything? Any research? – NewToJS Mar 24 '18 at 19:00
  • yes but without success.. I used the TextUtils in order to check if the edittext is empty but I don't know how to check if the user enterd restricted words or numeric sequence – Andrew Mar 24 '18 at 19:14

2 Answers2

0

Implement a TextWatcher to your EditText addTextChangedListener and on

beforeTextChanged(CharSequence s, int start, int count, int after)

check if the input is the one you trying to reject.

Estevex
  • 791
  • 8
  • 17
  • Thank you Estevex. Could you please explain how to use TextWatcher for this specific case? I'm trying to implement TextWatcher but somthing is wrong in my code..Thank you for patience.. Regards – Andrew Mar 24 '18 at 19:46
0

I've implemented this but something is wrong

private class TextChangeListener implements TextWatcher {
    //before text changed
    public void beforeTextChanged(CharSequence s, int start, int before, int count){

        if (editTextName.getText().toString() == "123456") ;
            editTextName.setError("Please enter correct words");
            editTextName.requestFocus();
            return;
    }
    //on text changed
    public void onTextChanged(CharSequence s, int start, int before, int count){
    }
    //after text changed
    public void afterTextChanged(Editable ed){

    }
}
Andrew
  • 3
  • 2
  • Instead of `editTextName.getText().toString() == "123456"` use something like `s.equals("123456")` – Estevex Mar 24 '18 at 22:06
  • Thank you very much Estevex! I've solved with this: `if (s.toString().matches("0|00|000|123|111|123|123456|1234|12345|9874"))` – Andrew Mar 25 '18 at 00:47