How the pattern works
| Part | Meaning |
|---|---|
^[A-Za-z] | Must start with a letter |
[A-Za-z0-9_]{2,15} | 2–15 more letters, digits or underscores (3–16 characters in total) |
$ | End of the string |
Code examples
JavaScript
const re = /^[A-Za-z][A-Za-z0-9_]{2,15}$/;
re.test(value); // true or falsePython
import re
bool(re.fullmatch(r'[A-Za-z][A-Za-z0-9_]{2,15}', value))PHP
preg_match('/^[A-Za-z][A-Za-z0-9_]{2,15}$/', $value) === 1;
Notes and limitations
Adjust the length and character set to your rules, and compare usernames case-insensitively so “Admin” and “admin” can’t both register.
Regex TesterType a pattern and watch matches highlight live in your test text.
Open the tool Frequently asked questions
What is the regex for username?
/^[A-Za-z][A-Za-z0-9_]{2,15}$/ — it matches the whole string. See the table above for what each part does.
Which examples match?
Of the examples tested, these match: john_doe, Dev2026, abc. The others don’t.
How do I test this regex?
Click “Open in Regex Tester” — the pattern, flags and sample text load automatically, and matches highlight as you edit.