Skip to content

Instantly share code, notes, and snippets.

@zenja
Created April 8, 2012 12:46
Show Gist options
  • Save zenja/2337108 to your computer and use it in GitHub Desktop.
Save zenja/2337108 to your computer and use it in GitHub Desktop.
Check if the username is valid by using regex
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();
}
}
@purushottam10
Copy link

your validation is good
but i want to add @ # $ & and -

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment