Issue
I'm trying to create a regular expression to validate usernames against these criteria:
- Only contains alphanumeric characters, underscore and dot.
- Underscore and dot can't be at the end or
start of a username (e.g
_username/username_/.username/username.). - Underscore and dot can't be next to each other (e.g
user_.name). - Underscore or dot can't be used multiple times in a row (e.g
user__name/user..name). - Number of characters must be between 8 to 20.
This is what I've done so far; it sounds it enforces all criteria rules but the 5th rule. I don't know how to add the 5th rule to this:
^[a-zA-Z0-9]+([._]?[a-zA-Z0-9]+)*$
Solution
^(?=.{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})[^_.].*[^_.]$
Answered By - Ωmega
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.