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
How to Sort by Two Fields in Java and Specify Sort Direction
Keytool Error Bash: Keytool: Command Not Found
Java to Jackson Json Serialization: Money Fields
How to Efficiently Read Multiple Json Files into a Dataframe or Javardd
Limiting the Number of Characters in a String, and Chopping Off the Rest
Appbar Not Showing Back Button
Method to Find the Smallest Number from an Integer
How to Compare Two Objects and Find the Fields/Properties Changed
How to Send Parallel Get Requests and Wait for Result Responses
Object Cannot Be Converted to Integer Error
Setting Default Values to Null Fields When Mapping With Jackson
Passing Empty List as Parameter to JPA Query Throws Error
How to Create a Java Code to Check Multiple Question
Java Error Message. Unexpected Type, Required Variable, Found Value
Prevent Empty Values on String Split
Jackson Polymorphism: How to Map Multiple Subtypes to the Same Class