DEV Community

Cover image for Username Validator
sndp
sndp

Posted on • Updated on

Username Validator

Usernames should be formatted and they should conform to follow some validation constraints.

As an example,

Type Constraints
Length of username Inclusive to 6 to 26 characters
Containing figures Only of alphanumeric characters
First figure being An alphabetical character

These usernames can be validated inside in register/sign up functionalities of your web application.

Now user enters an username as a string in the registration form provided.

Then they fill out the rest of info like email, password and confirm password etc. and submits.

After that you need to validate these user inputs.

To do this we have few options.

i. Using javascript regex to validate it before submitting the form. (With using jquery, React JS or other frontend js library)

ii. Using model attributes in your "User" view model class.
(With using [RegularExpression] for ASP.Net or @Pattern for Spring)

iii. Using server-side code to match and validate the pattern.
(This enables us to check the availability - uniqueness within the user table)

A recommended regex pattern (for Java) given below checks if the string input follows validation constraints given above.

String userRegex = "^[a-zA-Z][a-zA-Z0-9_]{5,25}+$";
Enter fullscreen mode Exit fullscreen mode
  • The tokens used in the regex are as below.
^
Enter fullscreen mode Exit fullscreen mode

This denotes the first character of the string.

[a-zA-Z]
Enter fullscreen mode Exit fullscreen mode

All the alphabetical and non-numerical chracters.

^[a-zA-Z]
Enter fullscreen mode Exit fullscreen mode

The first character being a alphabetical character.

[a-zA-Z0-9_]
Enter fullscreen mode Exit fullscreen mode

The rest of the string can be alphanumerical characters with no spaces.
(Alphabetical characters with mixed-case, integer numbers, underscores)

{5,25}
Enter fullscreen mode Exit fullscreen mode

This denotes the length constraint.
(The length of the string input should be between 6 and 26 inclusive. Since the first token of ^[a-zA-Z] if checked the length we checking reduced by one. So we need to check if it ranges from 5 to 25 for rest)

+
Enter fullscreen mode Exit fullscreen mode

This denotes that the whole pattern will be checked.

$ 
Enter fullscreen mode Exit fullscreen mode

This denotes the end of the pattern.

The following Java program matches if the given username is valid and conform to our ruleset.

public static void main(String[] args) {
   String userRegex = "^[a-zA-Z][a-zA-Z0-9_]{5,25}+$";
   java.util.Scanner sc = new java.util.Scanner(System.in);
   String userName = sc.nextLine();

   if (userName.matches(userRegex)) {
      System.out.println("Valid Username");
   } else {
      System.out.println("Invalid Username");
   }

   sc.close();
}
Enter fullscreen mode Exit fullscreen mode

Learn more about regex using following links

https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference

Top comments (1)

Collapse
 
justmedev profile image
justmedev

Use A-z covers ABCabc instead of A-Za-z