How to restrict the EditText to accept only alphanumeric characters

user3472001 picture user3472001 · Apr 22, 2014 · Viewed 75.9k times · Source

How can I restrict an EditText to accept only alphanumeric characters, with both lowercase and uppercase characters showing as uppercase in the EditText?

<EditText
    android:id="@+id/userInput"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="textMultiLine"
    android:minLines="3" >

    <requestFocus />
</EditText>

If a user types in lowercase "abcd", the EditText should automatically show uppercase "ABCD" without needing to restrict the keyboard to uppercase.

Answer

Hashir Sheikh picture Hashir Sheikh · Apr 22, 2014

In the XML, add this:

 android:digits="abcdefghijklmnopqrstuvwxyz1234567890 "