Created
April 8, 2012 12:46
-
-
Save zenja/2337108 to your computer and use it in GitHub Desktop.
Check if the username is valid by using regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class UsernameValidator{ | |
private Pattern pattern; | |
private Matcher matcher; | |
private static final String USERNAME_PATTERN = "^[a-z0-9_-]{3,15}$"; | |
public UsernameValidator(){ | |
pattern = Pattern.compile(USERNAME_PATTERN); | |
} | |
/** | |
* Validate username with regular expression | |
* @param username username for validation | |
* @return true valid username, false invalid username | |
*/ | |
public boolean validate(final String username){ | |
matcher = pattern.matcher(username); | |
return matcher.matches(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
your validation is good
but i want to add @ # $ & and -