Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 27, 2021 08:31 am GMT

Username Validator

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

As an example,

TypeConstraints
Length of usernameInclusive to 6 to 26 characters
Containing figuresOnly of alphanumeric characters
First figure beingAn 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 fills 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}+$";
  • The tokens used in the regex are as below.
^

This denotes the first character of the string.

[a-zA-Z]

All the alphabetical and non-numerical chracters.

^[a-zA-Z]

The first character being a alphabetical character.

[a-zA-Z0-9_]

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

{5,25}

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)

+

This denotes the whole pattern will be checked.

$ 

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();}

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


Original Link: https://dev.to/lizardkinglk/username-validator-1n8g

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To