Rosh_LK
Rosh_LK

Reputation: 700

validate user name-best regular expression

I came across this username guidelines from google

(https://support.google.com/a/answer/33386?hl=en ) .

When I am trying to validate usernames for my app with jQuery validate. I manged to achieve others except to detect two periods. Following is the regex I used.

/^[a-z0-9_.-]+$/i

How i can modify this to achieve those requirements.or is there any alternatives than using a single regular expression?

Upvotes: 0

Views: 529

Answers (1)

SamWhan
SamWhan

Reputation: 8332

Try with

/^(?!.*\.\.)[\w.-]{6,64}$/

( The 6 is some improv from me, assuming you'll want a minimum username length ;) )

It uses a negative looka-head to make sure there aren't two consecutive ., and then matches 6-64 allowed characters (\w matches a-z, A-Z, 0-9 and _).

Check it out here at regex101.

Upvotes: 2

Related Questions