cluv
cluv

Reputation: 620

regex to validate a username

I would like to validate a username with these parameters: "it may only contain alphanumeric characters or dashes and cannot begin with a dash and must have a length between 2-20 characters"

this is my attempt, but it's not quite right

/^[a-z0-9]+([a-z0-9-]+[a-z0-9])?$/i

thanks for any help

Upvotes: 0

Views: 931

Answers (1)

Explosion Pills
Explosion Pills

Reputation: 191749

/^[a-z0-9][-a-z0-9]{1,19}$/i

[^-] allows all sorts of non alphanumeric characters.

Upvotes: 4

Related Questions