Aftab Abbas
Aftab Abbas

Reputation: 57

How to make edit text accept only letters in android

I am validating an EditText and want to restrict input to [a-z][A-z] and a space.

For instance

Aftab Abbs

Not sure how to proceed, is there a way to achieve this?

Thanks

Upvotes: 0

Views: 98

Answers (1)

Sunny Kumar Sk
Sunny Kumar Sk

Reputation: 66

EditText state = (EditText) findViewById(R.id.txtState);

            Pattern ps = Pattern.compile("^[a-zA-Z ]+$");
            Matcher ms = ps.matcher(state.getText().toString());
            boolean bs = ms.matches();
            if (bs == false) {
                if (ErrorMessage.contains("invalid"))
                    ErrorMessage = ErrorMessage + "state,";
                else
                    ErrorMessage = ErrorMessage + "invalid state,";

            }

Upvotes: 1

Related Questions