Reputation: 303
There are so many questions and answers regarding this topic,but my problem is I have edit text in my app,there I need to enter a name,so I need to validate my edit text,to avoid spaces and special characters as the only name,and edit text can't be empty also.How can I do that without textwatcher.
Upvotes: 1
Views: 9617
Reputation: 67
edittext.setFilters(new InputFilter[] { new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
String regex = "a-z~@#$%^&*:;<>.,/}{+";
if(!dest.toString().contains(" ")||!dest.toString().matches("[" + regex + "]+")){
return null;
}
}
} });
Upvotes: -1
Reputation: 535
String username=edittext1.getText().toString();
if(username.matches("")) {
edittext1.setError("Invalid ");
}
add on xml file
android:inputType="textFilter"
may be it will help
Upvotes: 0
Reputation: 1368
You can use a simple regular expression
public boolean isValidWord(String word) {
return word.matches("[A-Za-z][^.]*");
}
if return value is true then your input dont have a special char or spaces.. if false it may have special char or spaces.. simple and elegant..
Just call the method with the string paramater before add your name..
Upvotes: 4
Reputation: 1711
You can use input filter to filter out special characters and white space
edittext.setFilters(new InputFilter[] { new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
String regex = "a-z~@#$%^&*:;<>.,/}{+";
if(!dest.toString().contains(" ")||!dest.toString().matches("[" + regex + "]+")){
return null;
}
}
} });
I hope it helps you..
Upvotes: 0