'Regular Expressions | Restrict Possible Usernames

Question

Here are some simple rules that users have to follow when creating their username.

1) Usernames can only use alpha-numeric characters.

2) The only numbers in the username have to be at the end. There can be zero or more of them at the end. Username cannot start with the number.

3) Username letters can be lowercase and uppercase.

4) Usernames have to be at least two characters long. A two-character username can only use alphabet letters as characters.

My Code

let username = "JackOfAllTrades";
let userCheck = /^(a-za-z|a-z(a-z+|\d\d+))(\d*)$/i; 
let result = userCheck.test(username);

My Question

How can I fix this code? What is it about the code that doesn't work?



Solution 1:[1]

Edit: I missed something in the OP requirements: there can be only one leading letter if the username is more than 2 characters long. So I corrected this answer accordingly, and we fundamentally get the same regex as Venkatesh's solution 2.


I supposed that you wish only non-accented characters.

With the regular expression /^[a-z]([a-z]+\d*|\d{2,})$/i (test it here), you get the following matches/failures (when testing one by one):

• Paul46:  matches
• 4frank:  fails
• mike:    matches
• jus6tin: fails 
• p87:     matches
• k9:      fails
• AL10:    matches

Solution 2:[2]

Solution 1:

let username = "JackOfAllTrades";
let userCheck = /^[a-z]([0-9][0-9]+|[a-z]+\d*)$/i;
let result = userCheck.test(username);

Code Explanation

  • ^ - start of input
  • [a-z] - first character is a letter
  • [0-9][0-9]+ - ends with two or more numbers
  • | - or
  • [a-z]+ - has one or more letters next
  • \d* - and ends with zero or more numbers
  • $ - end of input
  • i - ignore case of input

Solution 2:

let username = "JackOfAllTrades";
const userCheck = /^[a-z]([0-9]{2,}|[a-z]+\d*)$/i;
let result = userCheck.test(username);

Code Explanation

  • ^ - start of input

  • [a-z] - first character is a letter

  • [0-9]{2,0} - ends with two or more numbers

  • | - or

  • [a-z]+ - has one or more letters next

  • \d* - and ends with zero or more numbers

  • $ - end of input

  • i - ignore case of input

Solution 3:[3]

Here is my solution:

 /^[a-z][a-z]+$|^[a-z]+\w\d+$/i

/^ - at the beginning of the string, find

[a-z][a-z]+$ - at least 2 letters, could be more, till the end(this means that strings like test12 will not pass in this one)

| - OR (to the cases where it has more numbers and more than 2 characters ^[a-z]+ - beginning with any letter, can be more

\w - any character, can be a-z and 0-9, it is used this way to force the string to have at least 3 characters

\d+$ - ending with a chain with at least one number(if it doesn't have at least one number at the end, it will match in the first of the conditional)

/i - consider caps characters

Solution 4:[4]

Try this one:

let userCheck = /^[a-z]([a-z]+|[a-z]*[\d][\d])$/i;

Solution 5:[5]

let userCheck = /^[a-z]([a-z]+|[0-9]\d+)\d*$/i;

Above passes all test cases of this problem

Solution 6:[6]

The below solution works fine to find a username ased on the below conditions Usernames can only use alpha-numeric characters.

  1. The only numbers in the username have to be at the end. There can be zero or 2. more of them at the end. Username cannot start with the number.
  2. Username letters can be lowercase and uppercase.
  3. Usernames have to be at least two characters long. A two-character username can only use alphabet letters as characters.

let username = "JackOfAllTrades";
let userCheck = /^[a-z]+(\d\d+$|[a-z]+\d*$)/i; // Change this line
let result = userCheck.test(username);
console.log(result)

Description:

  1. ^[a-z]+ - to match one or more(+) alphabet([a-z]) in the beginning(^).
  2. \d\d+$ - to match the ending 2 or more(\d for one and \d+ for one or more) number if only one alphabet in the beginning.
  3. [a-z]+\d*$ - to match one or more alphabet along with 0 or more number at the end.
  4. i - flag for ignoring letter case
  5. | - sign to pick match both regex

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Venkatesh Hariharan
Solution 3 user202684
Solution 4 Demsource
Solution 5
Solution 6 cursorrux