How To Validate Username With Regular Expression ?

Username Regular Expression Pattern

^[a-z0-9_-]{3,15}$

Description

^ # Start of the line
[a-z0-9_-] # Match characters and symbols in the list, a-z, 0-9, underscore, hyphen
{3,15} # Length at least 3 characters and maximum length of 15
$ # End of the line
Whole combination is means, 3 to 15 characters with any lower case character, digit or special symbol “_-” only. This is common username pattern that’s widely use in different websites.

/**
* 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();

}