Regular expression to validate username
I guess you'd have to use Lookahead expressions here. http://www.regular-expressions.info/lookaround.html
Try
^[a-zA-Z0-9](_(?!(\.|_))|\.(?!(_|\.))|[a-zA-Z0-9]){6,18}[a-zA-Z0-9]$
[a-zA-Z0-9]
an alphanumeric THEN (
_(?!\.)
a _ not followed by a . OR
\.(?!_)
a . not followed by a _ OR
[a-zA-Z0-9]
an alphanumeric ) FOR
{6,18}
minimum 6 to maximum 18 times THEN
[a-zA-Z0-9]
an alphanumeric
(First character is alphanum, then 6 to 18 characters, last character is alphanum, 6+2=8, 18+2=20)
^(?=.{8,20}$)(?![_.])(?!.*[_.]{2})[a-zA-Z0-9._]+(?<![_.])$
└─────┬────┘└───┬──┘└─────┬─────┘└─────┬─────┘ └───┬───┘
│ │ │ │ no _ or . at the end
│ │ │ │
│ │ │ allowed characters
│ │ │
│ │ no __ or _. or ._ or .. inside
│ │
│ no _ or . at the beginning
│
username is 8-20 characters long
If your browser raises an error due to lack of negative look-behind support, use the following alternative pattern:
^(?=[a-zA-Z0-9._]{8,20}$)(?!.*[_.]{2})[^_.].*[^_.]$