How to restrict special characters from an Android EditText field?
6 Answers
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)
-
3This method gives me issue when also using android:imeOptions="actionNext" on ICS devices. The 'Next' button does not show up!!! – Ahmed Faisal Sep 06 '12 at 02:16
-
What should the user do if he wants to use other language than english? – Ahmad Arslan Apr 14 '15 at 08:14
-
is this case insensitive or sensitive? – Uniruddh Aug 05 '16 at 07:20
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 });
}

- 4,610
- 6
- 41
- 56
-
-
solution is to do check in loop for every character, as it is shown here: http://stackoverflow.com/a/7335559/1235910 – Palejandro Sep 30 '16 at 08:34
I have solved this problem in my answer: Prevent special symbols in EditText. You can change the regex as per your requirement.

- 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
-
1The 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
Just put below xml line inside edittext block.
android:digits="abc"
This will allow to enter only a,b and c characters.

- 101
- 5
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
}

- 192
- 1
- 9
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.

- 51
- 1
- 6