EDITTEXT FIELD ACCEPT ONLY LETTERS AND WHITE SPACES IN ANDROID

Ashish Pandey

Total Post:128

Points:898
Posted by  Ashish Pandey
 8127  View(s)
Ratings:
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?
  1. jayprakash sharma

    Post:117

    Points:821
    Re: EditText field accept only letters and white spaces in Android

    Try this:

    EditText yourEditText = (EditText) findViewById(R.id.yourEditText);
    yourEditText.setFilters(new InputFilter[] {
        new InputFilter() {
            @Override
            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
  1. Tanuj Kumar

    Post:134

    Points:940
    Re: EditText field accept only letters and white spaces in Android

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!