Regex to Validate a Username
Copy the pattern, type a candidate username below, and see whether it passes. Below the tester: the length and character rules and how to change them.
What this pattern matches
This validates a username of 3–16 characters made of letters and digits, with single dots or underscores allowed between segments but never at the start, end, or doubled.
How it works, part by part
(?=.{3,16}$) — a lookahead enforcing the total length of 3 to 16.
[a-zA-Z0-9]+ — the username must start with letters or digits.
(?:[._][a-zA-Z0-9]+)* — optional single-dot or underscore separators, each followed by more characters.
Changing the rules
Adjust {3,16} for a different length, or add characters to the separator class. Keeping the anchored start prevents names like .admin that can confuse routing.
FAQ
Does it reject dot..dot?
Yes. Each separator must be followed by a real character, so consecutive dots fail.
How do I change the length limit?
Edit the {3,16} inside the leading lookahead to your desired minimum and maximum.
Can a username start with an underscore?
No. It must begin with a letter or digit, so _leading is rejected.