0

I want to limit number of letter entry to 18 characters and also cannot use more than 3 number digits.

To limit the length I've already done my code but don't know what to do with 3 number digits.

<android.support.v7.widget.AppCompatEditText
                    android:id="@+id/name_et"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:maxLength="18"
                    android:layout_marginStart="5dp"
                    android:digits="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890 @#$"
                    android:layout_toEndOf="@+id/name_tv"
                    android:imeOptions="actionDone"
                    android:textColor="@android:color/black" />
Android Guy
  • 573
  • 1
  • 8
  • 18

1 Answers1

0

You can use textWatchers to check that user should not enter more then three digits in your input field:

           yourEdittext.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                String numberRegx = "(?:\D*\d){3}";
                String currentString = yourEdittext.getText().length();            
                    if (currentString.matches(numRegex)) {
                        System.out.println("Invalid: " + input);
                 }else{
                        System.out.println("Valid: " + input);
               }
            }
        });
Umair
  • 6,366
  • 15
  • 42
  • 50
  • showing errors.. String numberRegx = "(?:\D*\d){3}"; Error :: Illegal escape character in string literal – Android Guy Jul 27 '18 at 09:00
  • @AndroidGuy the problem is with `"\" (back slash)`. add 2 back slashes instead of one then try again ? i.e `"\\"` – Umair Jul 27 '18 at 09:10