This example will show you how to validate a username using Java regular expression. Validating a username is a very common requirement while designing and developing a web application.
How to validate username using Java regular expression?
There may be different requirements to validate a username. This example covers the most common requirement of them. All the usernames will be stored in a string and example requirements are as follows.
1) Username must be 8 to 20 characters in length
2) Username can only contain alphanumeric characters, numbers, underscore (_) and dot (.)
3) Username cannot start with underscore and dot
Let’s try to build the regular expression pattern for the above requirements. We want only alphanumeric characters so it should be a to z or A to Z. The numbers will between 0 to 9. In addition, we also want _ and . in the allowed characters.
Here is the initial pattern as per the requirements.
1 |
String strPattern = "^[a-zA-Z0-9_.]{8,20}$"; |
1 2 3 4 5 |
Where, ^ - start of the string [a-zA-Z0-9_.] – any character between a to z, A to Z, _ and . {8,20} - repeating 8 to 20 times $ - end of the string |
Let’s try out our username regex pattern.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.javacodeexamples.regex; public class ValidateUsernameExample { public static void main(String args[]){ String strPattern = "^[a-zA-Z0-9_.]{8,20}$"; String[] strUserNames = { "Smith19", "Jason_max", "bond", "JamesBond@007", "_michael_clarke" }; for(String strUserName : strUserNames){ if(strUserName.matches(strPattern)){ System.out.println(strUserName + " is valid"); }else{ System.out.println(strUserName + " is not valid"); } } } } |
Output
1 2 3 4 5 6 7 |
John is not valid Smith19 is not valid Smith198 is valid Jason_max is valid james.bond is valid JamesBond@007 is not valid _michael_clarke is valid |
The first username “john” and “Smith19” are not valid because they contain only 4 and 7 characters respectively. “JamesBond@007” is not valid because it contains @ symbol which is not allowed. All other usernames from the above example are valid as per the pattern.
The last user name “_michael_clarke” is invalid as per our requirement but it turns out to be valid as per our pattern. So looks like we are done with our first two requirements, but we are yet to implement the third requirement “username cannot start with an underscore (_) or dot (.)”.
Let’s modify our username regex pattern a bit as follows.
1 |
String strPattern = "^[a-zA-Z0-9][a-zA-Z0-9_.]{7,20}$"; |
Basically we want to check that at the start of the string we want only alphanumeric character as the first character by specifying “^[a-zA-Z0-9]”, which can be followed by “a-zA-Z0-9_.” characters 7 to 20 times. Remember that we have already specified the first character separately, so {8,20} has been changed to {7,20} now.
Let’s try the new expression.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
String strPattern = "^[a-zA-Z][a-zA-Z0-9_.]{7,20}$"; String[] strUserNames = { "John", "Smith19", "Smith198", "Jason_max", "james.bond", "JamesBond@007", "_michael_clarke", ".jasonbourne", }; for(String strUserName : strUserNames){ if(strUserName.matches(strPattern)){ System.out.println(strUserName + " is valid"); }else{ System.out.println(strUserName + " is not valid"); } } |
Output
1 2 3 4 5 6 7 8 |
John is not valid Smith19 is not valid Smith198 is valid Jason_max is valid james.bond is valid JamesBond@007 is not valid _michael_clarke is not valid .jasonbourne is not valid |
Before we conclude, let’s think about usernames like “a_____b__c”, “a._._._b”, “a_.b__.c”. These usernames are valid as per our defined requirements but in the real world, they might not. In short, an underscore (_) and a dot (.) cannot appear in sequence.
In order to satisfy this requirement, we will have to change our pattern a bit to fix multiple subsequent appearances of underscore and dot. We will use a negative lookahead for validating this like given below.
1 |
String strPattern = "^(?!.*[_.]{2})[a-zA-Z][a-zA-Z0-9_.]{7,20}$"; |
The pattern is the same except for the first part “(?!.*[_.]{2})”. This is a negative lookahead expression which checks that there should not be any combination of characters out of the [_.] in length of two (so that “..”, “._”, “_.” and “__” will be invalid).
And here is the final code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.javacodeexamples.regex; public class ValidateUsernameExample { public static void main(String args[]){ String strPattern = "^(?!.*[_.]{2})[a-zA-Z][a-zA-Z0-9_.]{7,20}$"; String[] strUserNames = { "John", "Smith19", "Smith198", "Jason_max", "james.bond", "JamesBond@007", "_michael_clarke", ".jasonbourne", "a______b", "a_____b__c", "a._._._b", "a_.b__.c", "a_b_c_d_e_f", "jason._bourne" }; for(String strUserName : strUserNames){ if(strUserName.matches(strPattern)){ System.out.println(strUserName + " is valid"); }else{ System.out.println(strUserName + " is not valid"); } } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
John is not valid Smith19 is not valid Smith198 is valid Jason_max is valid james.bond is valid JamesBond@007 is not valid _michael_clarke is not valid .jasonbourne is not valid a______b is not valid a_____b__c is not valid a._._._b is not valid a_.b__.c is not valid a_b_c_d_e_f is valid jason._bourne is not valid |
See Also: How to validate password using Java regular expression
This example is a part of the Java RegEx tutorial.
Let me know your views in the comments section below.