0

today I have a doubt about regex and android:digits property

Is there a way to set a regex in my layout?

something like

android:digits="^[A-Z0-9]+$"

This should allow only UPPERCASE LETTERS and NUMBERS but is not working like that

Thank you so much!

1 Answer 1

2

That property is to specify allowed characters. Surprisingly, it even allows alphabets, symbols, etc...

<EditText
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:inputType="number"
    android:digits="0123abc"
    android:ems="10"/>

This will limit the input to just 0, 1, 2, 3, a, b and c.

Is there a way to set a regex in my layout?

You're probably could but it is limited to EditTexts by using a custom attribute and implementing InputFilter. It is not just adding extra works but making everything far more complicated and I'm pretty sure this is not something that you want to hear.

This should allow only UPPERCASE LETTERS and NUMBERS

android:digits="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.