19

I'm trying to check that the first character of a username is capital, the following can be letters or numbers and at most 20 characters long. Can someone explain why my syntax is wrong?

/^[A-z][a-z0-9_-]{3,19}$/
Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
Sam
  • 2,309
  • 9
  • 38
  • 53

5 Answers5

32

Your first Z is not a capital Z.

/^[A-Z][a-z0-9_-]{3,19}$/
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • I tried this, but for some reason the first letter isn't being recognized as capital. I entered "ssss" and it matched. – Sam Nov 25 '11 at 22:14
  • Actually never mind, I was using a regex expression tool incorrectly. – Sam Nov 25 '11 at 22:19
10

Why can't you let the poor users pick their own usernames? What you should do is convert all caps to lowercase.

"User Name".toLowerCase();

But if you are truly evil, you should change that z to a Z:

/^[A-Z][A-Za-z0-9_-]{3,19}$/
Community
  • 1
  • 1
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
7

I would do it like this:

var firstChar = strToCheck.substring(0, 1);

if (firstChar == firstChar.toUpperCase()) {
    // it is capital :D
}
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
BronzeByte
  • 685
  • 1
  • 7
  • 11
6

Your first character needs to be A-Z, not A-z

So

/^[A-z][a-z0-9_-]{3,19}$/

Should be

/^[A-Z][a-z0-9_-]{3,19}$/

Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
0

You have a typo, the first z should be a capital -

/^[A-Z][a-z0-9_-]{3,19}$/
ipr101
  • 24,096
  • 8
  • 59
  • 61