How to Create Edittext Accepts Alphabets Only in Android

How to create EditText accepts Alphabets only in android?


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,";

}

Making an 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 "";
}
}
});

create edit text only accept or able to enter only alphabetical character from a-z only android

see the InputType in android this is the reference Input Type API

android EditText alphabet-only validation

I think you'd do better using InputFilter:

InputFilter filter = new InputFilter() { 
public CharSequence filter(CharSequence src, int start, int end,
Spanned d, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (!Character.isLetter(src.charAt(i))) {
return src.subSequence(start, i-1);
}
}
return null;
}
};

edit.setFilters(new InputFilter[]{filter});

Code modified from: How do I use InputFilter to limit characters in an EditText in Android?

Edittext only allow letters (programmatically)

You can use this code below:

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.isLetter(source.charAt(i))&&!Character.isSpaceChar(source.charAt(i))) {
return "";
}
}
return null;
}
};
edit.setFilters(new InputFilter[] { filter });

How to restrict the EditText to accept only alphanumeric characters

In the XML, add this:

 android:digits="abcdefghijklmnopqrstuvwxyz1234567890 "

Validation allow only number and characters in edit text in android

Instead of using your "manual" checking method, there is something very easy in Android:

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)) &&
!Character.toString(source.charAt(i)).equals("_") &&
!Character.toString(source.charAt(i)).equals("-"))
{
return "";
}
}
return null;
}
};

edittext.setFilters(new InputFilter[] { filter });

Or another approach: set the allowed characters in the XML where you are creating your EditText:

<EditText 
android:inputType="text"
android:digits="0,1,2,3,4,5,6,7,8,9,*,qwertzuiopasdfghjklyxcvbnm,_,-"
android:hint="Only letters, digits, _ and - allowed" />

EditText with only ascii letters. How?

I found at least semi-working solution based on https://stackoverflow.com/a/49710730/1063214

imeOptions="flagForceAscii"  

on EditText.

Now at least non-english keyboard is not shown (and digits,etc) are filtered anyway.



Related Topics



Leave a reply



Submit