33

How to restrict special characters from an Android EditText field?

Nicolas Kaiser
  • 1,628
  • 2
  • 14
  • 26
saravanan palpandi
  • 353
  • 1
  • 4
  • 8

6 Answers6

46

Have you tried adding the android:digits="abcde.....0123456789" attribute? Although the android:digits specify that it is a numeric field, it does work for me to set it to accept letters as well, and special characters as well (tested on SDK-7).

If this doesn't work then you'll have to implement KeyListener see: http://developer.android.com/reference/android/widget/TextView.html#setKeyListener(android.text.method.KeyListener)

Hunter S
  • 1,293
  • 1
  • 15
  • 26
alvin
  • 693
  • 7
  • 8
6

Just pass the EditText object to this method. Also add the values that is to be blocked in the variable blockCharacterSet.

private void disableSpecialChar(EditText editText) {
      final String blockCharacterSet = "<>";

      InputFilter filter = new InputFilter() {

       @Override
       public CharSequence filter(CharSequence source, int start, int end,
         Spanned dest, int dstart, int dend) {

        if (source != null && blockCharacterSet.contains(("" + source))) {
         return "";
        }
        return null;
       }
      };

      editText.setFilters(new InputFilter[] { filter });

     }
Arun PS
  • 4,610
  • 6
  • 41
  • 56
2

I have solved this problem in my answer: Prevent special symbols in EditText. You can change the regex as per your requirement.

Ashwin
  • 7,277
  • 1
  • 48
  • 70
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/16885840) – philantrovert Jul 31 '17 at 13:29
  • Clearly the linked solution shows how to restrict special characters in EditText. This has solved my problem which was same as mentioned in the question. – Ashwin Jul 31 '17 at 13:40
  • Okay, so you flag it as a duplicate if it is already answered with an accepted answer. – philantrovert Jul 31 '17 at 13:45
  • 1
    The questions are different. One is asked to solve a bug in Android's widget property while this question is about achieving a requirement. But the solution to both of the questions are same. They cannot (should not) be marked duplicate. – Ashwin Jul 31 '17 at 13:53
1

Just put below xml line inside edittext block.

android:digits="abc"

This will allow to enter only a,b and c characters.

1

Here's a Kotlin version including whitespace:

private val userNameFilter =
    InputFilter { source, start, end, dest, dStart, dEnd ->
        for (i in start until end) {
            if (!Character.isLetterOrDigit(source[i]) && !Character.isWhitespace(source[i])) {
                return@InputFilter ""
            }
        }
        null
    }
fetchSerotonin
  • 192
  • 1
  • 9
0

I created a class for more flexible settings of input constraints to EditText:

https://github.com/devapro/NumberWatcher

It is implementation only for numbers input, but you can change it for any of the types.

Arseniy
  • 51
  • 1
  • 6