4
  1. The only numbers in the username have to be at the end. There can be zero or more of them at the end.

  2. Username letters can be lowercase and uppercase.

  3. Usernames have to be at least two characters long. A two-letter username can only use alphabet letter characters.

I'm trying with this but I'm stalled. /\d+$\w+/gi

Machavity
  • 30,841
  • 27
  • 92
  • 100
Lomolo
  • 51
  • 1
  • 5
  • Speaking from experience, don't use a username, just use an email and a display name (for which they can put whatever they want). No-one wants to upkeep another username for another site. In addition, with usernames, you end up having to report each validation failure individually (username too long, username needs a capital, blah blah blah). Just "username invalid" is a bad UX. What makes it invalid? Skip it, use emails. – Kzqai May 05 '17 at 17:32

6 Answers6

4

/^[a-z]{2,}\d*$/i is:

^     : the begining
[a-z] : a character (a to z), you can add as many allowed characters as you want
{2,}  : at least 2 of them
\d*   : 0 or more digits 
$     : the end
i     : ignore case sensetivity (both lowercases and uppercases are allowed)
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
2

Username having characters and digit and min 2 character long

/^[a-zA-Z]{2,}\d*$/i

Test result :

UserNam9 = pass
9username = fail
Userna99 = pass
usernameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee = pass
Us = pass
U = fail
  • this regex is wrong, and it has several issues. First, in this way you allow numbers also at the beginning. Then the i it's useless if you specify a-zA-Z. It makes sense if you use only a-z. Moreover in this way you accept also just two numbers. – quirimmo May 05 '17 at 17:34
  • What? You are right that the `[a-zA-Z]` is unnecessary, not wrong, but unnecessary. But the rest of it is right... – Jordan Soltman May 05 '17 at 18:23
0
/^[A-z]{2,}[A-z0-9]{0,}$/
/^ // start of line
[A-z]{2,} //alphabet characters 2 or more
[A-z0-9]{0,} //numbers and alphabet 
$/ // end of line
Osama
  • 2,912
  • 1
  • 12
  • 15
0

You've missed cases when there's a letter in the start, followed by 2 or more numbers.

U99 = fail
d345 = fail

My solution passes these tests, as well:

/^[a-z]{2,}\d*$|(?=\w{3,})^[a-z]{1,}\d+$/i

Using positive lookahead I am making sure that in the second case there are at least 3 alphanumeric characters.

0

Simplified version of /^[a-z]{2,}\d*$|(?=\w{3,})^[a-z]{1,}\d+$/i:

/^\D(\d{2,}|\D+)\d*$/i

Code explanation:

  1. ^ - start of input
  2. \D - first character is a letter
  3. \d{2,} - ends with two or more numbers
  4. | - or
  5. \D+ - has one or more letters next
  6. \d* - and ends with zero or more numbers
  7. $ - end of input
  8. i - ignore case of input
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Ay Jam
  • 1
  • 1
0

This is my answer, it passed all the tests:

/^[a-z][a-z]+\d*$|^[a-z]\d{2,}$/i
  • First Part: 2 letters (or more) and zero or more numbers
  • Or
  • Second Part: 1 letter and 2 or more numbers