0

I have the pattern of number police like this "^[A-Z]{1,2}\s\d{1,4}\s[A-Z]{1,3}$" How to implement that pattern tobe text watcher on edit text.

I want to fill in the automatic edit text according to the format

1 Answers1

0

this is my Email check code. check this example.

String preText;
...

TextWatcher emailTextWacher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
           preText = s.toString();
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String strEmailConfirm = s.toString();
            if(strEmailConfirm.equals(preText)) return;

            if (isValidEmail(strEmailConfirm)) {
                setEnableNextBtn();
            } else {
                setDisableNextBtn();
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

...


public static boolean isValidEmail(String email) {
        boolean err = false;
        String regex = "^[_a-z0-9-]+(.[_a-z0-9-]+)*@(?:\\w+\\.)+\\w+$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(email);
        if (m.matches()) {
            err = true;
        }
        return err;
    }

just make method for check pattern and return boolean, and use this in textwacher. you can modify for police number

S T
  • 1,068
  • 2
  • 8
  • 16