Regular expression to validate username Regular expression to validate username php php

Regular expression to validate username


^(?=.{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})[^_.].*[^_.]$


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)


As much as I love regular expressions I think there is a limit to what is readable

So I would suggest

new Regex("^[a-z._]+$", RegexOptions.IgnoreCase).IsMatch(username) &&!username.StartsWith(".") &&!username.StartsWith("_") &&!username.EndsWith(".") &&!username.EndsWith("_") &&!username.Contains("..") &&!username.Contains("__") &&!username.Contains("._") &&!username.Contains("_.");

It's longer but it won't need the maintainer to open expresso to understand.

Sure you can comment a long regex but then who ever reads it has to rely on trust.......