Regex for Username Validation

A username regex allowing letters, numbers and underscores with 3–16 characters. Test valid and invalid names and adapt the rules to your app.

Part of the free Regex Tester. Runs in your browser — nothing is uploaded.

Regex for Username Validation

A simple, predictable rule set works best for usernames: allowed characters and a length range. This pattern accepts ASCII letters, digits and underscores from 3 to 16 characters.

Worked example: Usernames against the rule

Pattern /^[a-zA-Z0-9_]{3,16}$/ — test strings

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

ada_lovelace
user123
ab
has space
way_too_long_username_here

Result for each line

✓ match     ada_lovelace
✓ match     user123
✗ no match  ab
✗ no match  has space
✗ no match  way_too_long_username_here

Practical tip

Normalize case before checking uniqueness, so "Ada" and "ada" cannot both register, and consider reserving names such as admin and support.

How to use this Regex Tester

  1. 1Use the live tool above — it runs entirely in your browser and never uploads your data.
  2. 2Paste the sample from the worked example, or use your own input.
  3. 3Copy the result, then adapt the same steps in your editor, script, or pipeline.

Regex for Username Validation FAQs

How do I allow dots or hyphens?

Add them to the character class: [a-zA-Z0-9_.-]. Place the hyphen last or escape it so it is not read as a range.

Does this allow non-English letters?

No, it is ASCII only. Use the u flag and \p{L} to allow letters from any language.

More Regex Tester pages

Open the full Regex Tester tool →