Search code examples
androidvalidationandroid-edittext

How to restrict the EditText to accept only alphanumeric characters


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.


Solution

  • In the XML, add this:

     android:digits="abcdefghijklmnopqrstuvwxyz1234567890 "