How to Do Name Validation Allowing Alphabet, Spaces and Dot in Android

How to do name validation allowing alphabet, spaces and dot in android?

Try using this one instead [a-zA-Z.\s]+ - allowing alphabet or dot or whitespaces.

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

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" />

Java/Android Regex expression to allow names that may have apostrophe and/or hyphen

Use this:

^[a-zA-Z'\\-\\s]+$

I've tested it now and it works (already escaped for Java, don't add more slashes)

Pattern pattern = Pattern.compile("^[a-zA-Z'\\-\\s]+$");
Matcher matcherFirstName = pattern.matcher("M'ontagu-S'tuart-W'ortley-M' ackenzie");
matcherFirstName.find(); //true

Regular Expression for alphabets with spaces

Just add the space to the [ ] :

var regex = /^[a-zA-Z ]*$/;

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

}


Related Topics



Leave a reply



Submit