Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zenius/87d75fc7d61826f71fa8bcde0e9813b5 to your computer and use it in GitHub Desktop.
Save zenius/87d75fc7d61826f71fa8bcde0e9813b5 to your computer and use it in GitHub Desktop.
Usernames are used everywhere on the internet.
They are what give users a unique identity on their favorite sites.
You need to check all the usernames in a database.
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.
Solution:
let username = "JackOfAllTrades";
let userCheck = /^[a-z]([a-z]+\d*|\d{2,})$/i;
let result = userCheck.test(username);
@DiBrown
Copy link

DiBrown commented Aug 28, 2024

Thank you Cema for that explanation... one question though... it says: 4) Usernames have to be at least two characters long.
A two-character username can only use alphabet letters as characters.

You said \d{2,} enforces all numbers. Isn't that incorrect? Although it worked for me...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment