Back to the tool

Username Validation Regex: The Pattern That Rejects the Right Things

The username regex most tutorials hand you is /^[a-zA-Z0-9_]+$/. It accepts a lone underscore. It accepts a 400-character string. It has no opinion about either. Here is the version I actually use, and the rules behind each piece of it.

/^[a-zA-Z0-9_-]{3,20}$/

That one line makes four decisions. The ^ and $ anchors force the pattern to match the entire string, not just find a match inside it. Without anchors, "ab!!!" passes, because the engine happily finds "ab" in the middle and calls it a day. The character class allows letters, digits, underscores, and hyphens, and nothing else. The {3,20} sets the length window: no two-letter names, no essays.

InputResultWhy
dev-42PassLetters, hyphen, digits; within length
maria_99PassUnderscore is allowed
abFailBelow the 3-character minimum
has spacesFailSpaces are not in the class
name!Fail! is not in the class
_leadingPass (but see below)The basic pattern allows it; the strict one does not

Username validation regex best practice: write the rules before the pattern

The pattern is the easy part. The hard part is deciding what a username is allowed to be on your site, because every product answers differently. Here are the rules I recommend as a starting point, and why each one earns its place:

3 to 20 characters. Long enough to be a name, short enough to display everywhere. Most sites land in this window; GitHub allows up to 39, which tells you the ceiling is a product decision, not a technical one.

Letters, digits, underscore, hyphen. This set survives URLs, @mentions, and database columns without escaping. Dots are the usual temptation, and they cause the usual trouble: john.doe looks fine until it meets a system that splits on dots.

Must start with a letter. Usernames that begin with digits or underscores look broken in profile URLs and mentions. The strict version enforces it:

/^[a-zA-Z][a-zA-Z0-9_-]{2,19}$/

No leading, trailing, or doubled special characters. _maria_ and maria--99 are technically in the class but look like typos. A negative lookahead keeps them out:

/^(?!.*[_-]{2})[a-zA-Z0-9_-]{3,20}$/

What the regex cannot do

A pattern checks shape. It cannot tell you whether maria_99 is already taken. Uniqueness is a database query, and no character class will ever replace it. Reserved words are the same story: admin, root, and support all pass every pattern above, so they need a blocklist, not a better regex. And whether Maria and maria are the same user is a product decision you make before writing the pattern, not something the pattern can decide for you.

One more comfort: this class of pattern cannot blow up on you. Catastrophic backtracking needs nested quantifiers and ambiguity; a flat character class with a fixed length range matches in linear time. If you want the gory details of what backtracking looks like when it does go wrong, that is a different article.

Write your five rules on paper first. Pick the variant that matches them. Then build the pattern and test it against the weirdest usernames you can think of, because your users will think of weirder ones.

Frequently asked questions

Should usernames allow spaces?

Almost never. Spaces break profile URLs, complicate @mentions, and cause endless trimming bugs. If display names need spaces, keep them as a separate field from the username.

How long should a username be allowed to be?

3 to 20 characters covers most products. GitHub allows up to 39, which shows the ceiling is a product decision. Pick a maximum your UI can display without truncating.

Do I need the ^ and $ anchors?

Yes. Without them the pattern only needs to match part of the string, so "ab!!!" passes because the engine finds "ab" inside it. Anchors are what make it validation instead of searching.

Can a regex check whether a username is taken?

No. Uniqueness requires a database lookup at signup time. Regex checks the shape of the string; only your user table knows whether the name exists.

Should I allow dots in usernames?

I would not. Dots look harmless but collide with systems that split identifiers on dots, and "john.doe" versus "johndoe" creates impersonation-adjacent confusion. Letters, digits, underscore, and hyphen are enough.

Related reading: Why There Is No Perfect Email Regex · Phone Number Regex: Why You Should Not Write Your Own · Catastrophic Backtracking, Explained

Try the tool

Build and test the username pattern against your own inputs. Build a regex pattern with plain-English explanations.

Get new free tools by email

Want the next guide in your inbox? I publish one practical guide per new tool. Subscribe to the free newsletter on Substack. No spam, unsubscribe anytime.