I want to filter the input of an EditText
, only digits and letters are allowed, first I use TextWatcher
to deal with the last input character, but when you move the cursor or you past some content to the EditText
, this method failed, now I want to know is there a way to filter the illegal input and give the user a feedback.
Asked
Active
Viewed 2.6k times
6

twlkyao
- 14,302
- 7
- 27
- 44
-
Check out : https://github.com/vekexasia/android-edittext-validator – Haresh Chhelana May 15 '15 at 12:30
3 Answers
15
Add InputFilter
to your EditText
& provide a Toast
for user . This code snippet will help you.
InputFilter filter = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (!Character.isLetterOrDigit(source.charAt(i))) { // Accept only letter & digits ; otherwise just return
Toast.makeText(context,"Invalid Input",Toast.LENGTH_SHORT).show();
return "";
}
}
return null;
}
};
editText.setFilters(new InputFilter[] { filter });

Don Chakkappan
- 7,397
- 5
- 44
- 59
-
Could the 'InputFilter' confilts with the 'TextWatcher', I use `TextWatcher` to control the a button state with the length of the `EditText` at the same time, but with the `InputFilter`, the `InputFilter` doesn't work. – twlkyao May 15 '15 at 13:11
-
I have used the same code and works fine. Please post your complete code – Don Chakkappan May 15 '15 at 13:14
-
-
@twlkyao It is very happy to see that your problem is solved .Cheers – Don Chakkappan May 18 '15 at 07:49
6
Have you tried the xml way? It will look something like this
<EditText
android:id="@+id/editText"
android:inputType="text"
android:digits="0,1,2,3,4,5,6,7,8,9,*,qwertzuiopasdfghjklyxcvbnm"
android:hint="Only letters and numbers allowed.."
/>

Bidhan
- 10,607
- 3
- 39
- 50
-
-
There is hint however. What kind of feedback are you looking for? A toast message? – Bidhan May 15 '15 at 12:29
-
This is awesome. I'm pretty sure if you're entering for example a telephone it's pretty obvious if only numbers and - work or something like that. No feedback needed. – csga5000 Apr 14 '16 at 05:17
-
2Commas "," are not necessary. You can write `android:digits="0123456789*qwertzuiopasdfghjklyxcvbnm"` – Rafa0809 Oct 25 '16 at 23:47
-
i want to use alphabets digits and spaces then what to do? @Rafa0809 – Shashwat Gupta Aug 01 '17 at 10:10
-
1@ĜüptåŠhãsĥwæt Try this: android:digits="0123456789abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ " – Rafa0809 Aug 01 '17 at 11:38
-
-
Have you ensured you included the space in the middle of the android:digits? Otherwise the problem is not focused on the android:digits. Take a look at this post: https://stackoverflow.com/questions/26057279/making-an-edittext-field-accept-only-letters-and-white-spaces-in-android – Rafa0809 Aug 02 '17 at 08:48
6
Work on all Android Versions:
public static InputFilter getOnlyCharactersFilter() {
return getCustomInputFilter(true, false, false);
}
public static InputFilter getCharactersAndDigitsFilter() {
return getCustomInputFilter(true, true, false);
}
public static InputFilter getCustomInputFilter(final boolean allowCharacters, final boolean allowDigits, final boolean allowSpaceChar) {
return new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
boolean keepOriginal = true;
StringBuilder sb = new StringBuilder(end - start);
for (int i = start; i < end; i++) {
char c = source.charAt(i);
if (isCharAllowed(c)) {
sb.append(c);
} else {
keepOriginal = false;
}
}
if (keepOriginal) {
return null;
} else {
if (source instanceof Spanned) {
SpannableString sp = new SpannableString(sb);
TextUtils.copySpansFrom((Spanned) source, start, sb.length(), null, sp, 0);
return sp;
} else {
return sb;
}
}
}
private boolean isCharAllowed(char c) {
if (Character.isLetter(c) && allowCharacters) {
return true;
}
if (Character.isDigit(c) && allowDigits) {
return true;
}
if (Character.isSpaceChar(c) && allowSpaceChar) {
return true;
}
return false;
}
};
}
Now you can use this filer like:
//Accept Characters Only
edit_text.setFilters(new InputFilter[]{getOnlyCharactersFilter()});
//Accept Digits and Characters
edit_text.setFilters(new InputFilter[]{getCharactersAndDigitsFilter()});
//Accept Digits and Characters and SpaceBar
edit_text.setFilters(new InputFilter[]{getCustomInputFilter(true,true,true)});

Salam El-Banna
- 3,784
- 1
- 22
- 34