I need to validate a user’s screen name to make sure that it can not have more than one hyphen or underscore I don’t want people’s screen names to be all punctuation.
This is the validation I have so far:
public boolean validateScreenName(String screenName) {
// Check screen name has > 0 chars and that it contains only a-z, A-Z, _ and -
boolean validated = false;
if (screenName.matches("[a-zA-Z0-9_-]{1,20}")
&& (! screenName.equals(""))
&& (! screenName.contains("\\s"))) {
validated = true;
} else {
validated = false;
}
return validated;
}
I want to add the no-multiple-hyphen/underscore validation as another if condition maybe in the form of a RegEx?
You could use:
The other conditions are not needed, they are redundant.