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

Fi…


This content originally appeared on DEV Community and was authored by sndp

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 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


This content originally appeared on DEV Community and was authored by sndp


Print Share Comment Cite Upload Translate Updates
APA

sndp | Sciencx (2021-11-27T08:31:09+00:00) Username Validator. Retrieved from https://www.scien.cx/2021/11/27/username-validator/

MLA
" » Username Validator." sndp | Sciencx - Saturday November 27, 2021, https://www.scien.cx/2021/11/27/username-validator/
HARVARD
sndp | Sciencx Saturday November 27, 2021 » Username Validator., viewed ,<https://www.scien.cx/2021/11/27/username-validator/>
VANCOUVER
sndp | Sciencx - » Username Validator. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/27/username-validator/
CHICAGO
" » Username Validator." sndp | Sciencx - Accessed . https://www.scien.cx/2021/11/27/username-validator/
IEEE
" » Username Validator." sndp | Sciencx [Online]. Available: https://www.scien.cx/2021/11/27/username-validator/. [Accessed: ]
rf:citation
» Username Validator | sndp | Sciencx | https://www.scien.cx/2021/11/27/username-validator/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.