I need a function to return true or false, depending on the amount of letters

Asked

Viewed 116 times

-2

boolean b = mascara.matches("\\D{0,4}");

I need a mask (like for example "GHO-1A23") is verified to contain only 4 letters.

If you have 4 type only number and if you have less than 4 let type one more letter.

But this way above, it returns false when the user will type a next letter already with the mask being "GHO-1".

  • Thank you! I wasn’t getting to put as code

  • To help center has several tips for formatting the code. And \\D{0,4} checks if the string contains between zero and 4 characters that are not digits. That is, if it has 1 letter, it works. If it has none, it also works. But if it has a digit, it doesn’t work. It doesn’t check if it has exactly 4 letters. Another detail is that matches checks if the whole string matches the expression. If the mask is GHO-1, it has a digit and so does not correspond to \\D{0,4} (between zero and 4 characters that are not digits) - so returns false

  • 1

    According to a question performed by you yesterday. Vc wanted to validate boards. But the code you were creating was to insert a plate mask. You need to be a little more objective in the question, because the answer you get might not meet. Just to try to help you a little, in the real world, when you have Mediterranean plates, you will still have old model plates LLL-NNNN (L - letter and N - number). How your code will meet the 2 possibilities?

  • I solved the problem. The complete class solves the two possibilities.

  • 1

    Then create an answer by placing the code. It can help future users who have the same question as yours.

1 answer

2

So I changed the regex expression:

mascara.matches("\\D{0,4}|\\D{0,4}\\d{0,2}|\\D{0,4}\\d{0,3}"); 

My test has handled all the cases, including the old plates, but if anyone finds any disagreement, I’ll be happy to let you know.

import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.Toast;



public class PlacaUtils {

    HomeActivity context;

    public static String unmask(String s) {
        return s.replaceAll("[.]", "").replaceAll("[-]", "")
                .replaceAll("[/]", "").replaceAll("[(]", "")
                .replaceAll("[)]", "");
    }

    public static TextWatcher insert(final EditText ediTxt) {
        return new TextWatcher() {
            String mask = "UUU-####";
            boolean isUpdating;
            String old = "";


            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                ediTxt.setFilters(new InputFilter[]{new InputFilter.AllCaps()});
                String str = PlacaUtils.unmask(s.toString());
                String mascara = "";
                if (isUpdating) {
                    old = str;
                    isUpdating = false;
                    return;
                }
                int i = 0;
                for (char m : mask.toCharArray()) {
                    if ((m != '#' && m != 'U') && str.length() > old.length()) {
                        mascara += m;
                        continue;
                    }
                    try {
                        Character c = str.charAt(i);
                        if (mascara.length() < 3) {
                            if (Character.isLetter(c)) {
                                mascara += str.charAt(i);
                            }
                        } else if (mascara.length() >= 3) {
                            if (Character.isDigit(c)) {
                                mascara += str.charAt(i);
                            } else if (Character.isLetter(c)) {
                                boolean b = mascara.matches("\\D{0,4}|\\D{0,4}\\d{0,2}|\\D{0,4}\\d{0,3}");
                                if (b) {
                                    mascara += str.charAt(i);
                                }

                            }
                        }
                    } catch (Exception e) {
                        break;
                    }
                    i++;
                }
                isUpdating = true;
                if (str.length() < 3) {
                    ediTxt.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
                } else if (str.length() >= 3) {
                    ediTxt.setInputType((InputType.TYPE_CLASS_TEXT));
                }

                ediTxt.setText(mascara);
                ediTxt.setSelection(mascara.length());


            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            public void afterTextChanged(Editable s) {
            }
        };
    }

    public static boolean isValid(final String placa) {
        if (StringUtils.isEmpty(placa)) {
            return false;
        }

        if (placa.matches("[A-Z]{3}[0-9][A-Z][0-9]{2}")) {
            return true;
        } else {
            return false;
        }
    }

    public static String masked(String placa) {

        placa = unmask(placa);
        placa = placa.substring(0, 3) + "-" + placa.substring(3, 7);

        return placa;
    }
}
  • 1

    If decided, remember to check how accepted later to close the post

  • 1

    Take advantage and remove your old question, which was similar to this, then the site gets more organized and you recover your points.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.