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.
1. Java Regular Expression Example
UsernameValidator.java
package com.javaSchool.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(); } }
2. Username that match:
1. javaSchool34
2. javaSchool_2002
3. javaSchool-2002
4. js3-4_cool
2. javaSchool_2002
3. javaSchool-2002
4. js3-4_cool
3. Username that doesn’t match:
1. js (too short, min 3 characters)
2. js@cool (“@” character is not allow)
3. javaSchool123456789_- (too long, max characters of 15)
2. js@cool (“@” character is not allow)
3. javaSchool123456789_- (too long, max characters of 15)
4. Unit Test – UsernameValidator
Using testNG to perform unit test.
UsernameValidatorTest.java
package com.javaSchool.regex; import org.testng.Assert; import org.testng.annotations.*; /** * Username validator Testing * @author javaSchool * */ public class UsernameValidatorTest { private UsernameValidator usernameValidator; @BeforeClass public void initData(){ usernameValidator = new UsernameValidator(); } @DataProvider public Object[][] ValidUsernameProvider() { return new Object[][]{ {new String[] { "javaSchool34", "javaSchool_2002","javaSchool-2002" ,"mk3-4_yong" }} }; } @DataProvider public Object[][] InvalidUsernameProvider() { return new Object[][]{ {new String[] { "js","js@cool","javaSchool123456789_-" }} }; } @Test(dataProvider = "ValidUsernameProvider") public void ValidUsernameTest(String[] Username) { for(String temp : Username){ boolean valid = usernameValidator.validate(temp); System.out.println("Username is valid : " + temp + " , " + valid); Assert.assertEquals(true, valid); } } @Test(dataProvider = "InvalidUsernameProvider", dependsOnMethods="ValidUsernameTest") public void InValidUsernameTest(String[] Username) { for(String temp : Username){ boolean valid = usernameValidator.validate(temp); System.out.println("username is valid : " + temp + " , " + valid); Assert.assertEquals(false, valid); } } }
5. Unit Test – Result
Username is valid : javaSchool34 , true Username is valid : javaSchool_2002 , true Username is valid : javaSchool-2002 , true Username is valid : mk3-4_yong , true username is valid : js, false username is valid : js@cool , false username is valid : javaSchool123456789_- , false PASSED: ValidUsernameTest([Ljava.lang.String;@1d4c61c) PASSED: InValidUsernameTest([Ljava.lang.String;@116471f) =============================================== com.javaSchool.regex.UsernameValidatorTest Tests run: 2, Failures: 0, Skips: 0 =============================================== =============================================== javaSchool Total tests run: 2, Failures: 0, Skips: 0 ===============================================
Sign up here with your email
ConversionConversion EmoticonEmoticon