Home > Software engineering >  Regex in android-digits
Regex in android-digits

Time:02-22

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!

CodePudding user response:

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"
  • Related