I have an EditText field in my project which stands for the full name of the person.So I want only letters and spaces to be allowed in it.So I tried the following in the
Try this solution, it block entering number, special characters except space.
editText.setFilters(new InputFilter[] {
new InputFilter() {
@Override
public CharSequence filter(CharSequence cs, int start,
int end, Spanned spanned, int dStart, int dEnd) {
return cs.toString().replaceAll("[^a-zA-Z ]*","");
}
}
});
Kotlin Version:
editText.filters = arrayOf(
InputFilter { source, start, end, dest, dstart, dend ->
return@InputFilter source.replace(Regex("[^a-zA-Z ]*"), "")
}
)