Skip to content

Java Regular Expression Validate Username Example

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.

Let’s try out our username regex pattern.

Output

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.

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.

Output

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.

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.

Output

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.

About the author

Leave a Reply

Your email address will not be published.