Home > DeveloperSection > Forums > EditText field accept only letters and white spaces in Android
Ashish Pandey

Total Post:128

Posted on    October-19-2014 11:34 PM

 Android Android  Java  Validation 

 2 Reply(s)
 7981  View(s)
Rate this:
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 XML file

android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "

But it didn't work.Can anyone tell me how to do it?

jayprakash sharma
jayprakash sharma

Total Post:117

Posted on    October-19-2014 11:35 PM

Try this:

EditText yourEditText = (EditText) findViewById(R.id.yourEditText);
yourEditText.setFilters(new InputFilter[] {
    new InputFilter() {
        public CharSequence filter(CharSequence cs, int start,
                    int end, Spanned spanned, int dStart, int dEnd) {
            // TODO Auto-generated method stub
            if(cs.equals("")){ // for backspace
                 return cs;
            if(cs.toString().matches("[a-zA-Z ]+")){
                 return cs;
            return "";

Modified On Mar-15-2016 05:30:34 AM

Tanuj Kumar

Total Post:134

Posted on    October-19-2014 11:37 PM

use regex.. pattern should be

Full_Name_Pattern = "[A-Z][a-z]+( [A-Z][a-z]+)*";

Modified On Mar-15-2016 05:40:49 AM

Don't want to miss updates? Please click the below button!

Follow MindStick