T
ToolPrime

Regex for Username

Validates usernames: starts with a letter, 3-20 characters.

Pattern

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

Live Tester

Enter a string to test

Examples

john_doe
User123
my-name
1user
ab
user name

Pattern Breakdown

^[a-zA-Z] — must start with a letter

[a-zA-Z0-9_-]{2,19}$ — followed by 2-19 valid characters

Code Snippets

Javascript

const regex = /^[a-zA-Z][a-zA-Z0-9_-]{2,19}$/;
regex.test("john_doe"); // true

Python

import re
pattern = r"^[a-zA-Z][a-zA-Z0-9_-]{2,19}$"
bool(re.match(pattern, "john_doe"))  # True

Php

$pattern = '/^[a-zA-Z][a-zA-Z0-9_-]{2,19}$/';
preg_match($pattern, "john_doe"); // 1

Related Patterns

Frequently Asked Questions

What is the regex for Username?
The regex pattern for Username is /^[a-zA-Z][a-zA-Z0-9_-]{2,19}$/. Validates usernames: starts with a letter, 3-20 characters.
How do I use the Username regex in JavaScript?
In JavaScript you create the pattern with a regex literal or the RegExp constructor and call .test() to check a string against it. A ready-to-copy example is shown in the Code Snippets section above, alongside Python and PHP versions.
What does the Username pattern match?
It matches strings like john_doe; it rejects strings like 1user. See the Examples section above for the full list of matching and non-matching cases.

Related Tools