Username Regular Expression

Username Regular Expression
Username Regular Expression

Most of the time while working on projects in software/web/mobile development we have to deal with the validations while filling forms and if we are allowing users to fill in their information then it is must that the user should fill the correct information as per our wish. Let us say that we need to allow the user registration for to accept the ‘username’ which will have one decimal sandwiched with alphanumeric characters like ‘’. So to right the regular expression for this text you must have the knowledge that how regular expression works which we have already explained in our previous articles of regular expressions.


will allow you to enter username which will have the decimal sandwiched between alphanumeric characters.

In this regular expression everything which we are mentioning is must and not optional. Set of ‘/’ will allow you the starting and ending of a regular expression while ‘g’ means global search which retains the index of last match and allows iterative searches.

() is a capturing group which groups multiple tokens together and creates a capture group for substring extraction.

[] denotes the character class and here ([a-zA-Z0-9]) will match the alphanumeric characters including upper & lowercase characters.

{1,} for alowing alphanumeric character of length 1 or more, so whether the name before ‘.’ is of 1 character or is of more than 1, it will match all these cases.

\. is for the decimal or a period

+ will match for 1 or more of preceding token.

Difference of (+ or *) is very easy to understand here that if you will use ‘*’ then the regex will allow the username “steve.” on the other hand if you will use ‘+’ then it will not allow “steve.” but will allow “steve.j” because in case of ‘+’ there must be atleast one character token of preceding token.

If you want to put the limit on characters then you can write the regular expression as:


this will limit both the words (pre and post decimal) to minimum of 1 to maximum of 15.

You may also like...

Leave a Reply