Username Validation Regex: Alphanumeric Pattern
• CronOS Team
regexusernamevalidationtutorialauthentication
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!
Username Validation Regex: Alphanumeric Pattern
Learn how to validate usernames with the regex pattern ^[a-z0-9_-]{3,16}$ for alphanumeric usernames with underscores and hyphens.
Pattern Breakdown
regex
^[a-z0-9_-]{3,16}$
Components
| Component | Description | Matches |
|---|---|---|
^ | Start anchor | Ensures match from string start |
[a-z0-9_-] | Character class | Lowercase letters, digits, underscore, hyphen |
{3,16} | Length quantifier | Between 3 and 16 characters (inclusive) |
$ | End anchor | Ensures match to string end |
Character Classes
a-z- Lowercase letters (a through z)0-9- All digits (0 through 9)_- Underscore character-- Hyphen character (placed at end to avoid range interpretation){3,16}- Quantifier: between 3 and 16 occurrences (inclusive)
Examples
Valid:
john_doeuser123admin-usertest_123abcverylongusername123
Invalid:
ab(too short, minimum 3 characters)this_username_is_too_long(too long, maximum 16 characters)JohnDoe(uppercase letters not allowed)user name(spaces not allowed)user@name(special characters not allowed except _ and -)user.name(dots not allowed)
Implementation
JavaScript
javascript
const usernameRegex = /^[a-z0-9_-]{3,16}$/;
usernameRegex.test('john_doe'); // true
usernameRegex.test('user123'); // true
usernameRegex.test('ab'); // false (too short)
usernameRegex.test('this_username_is_too_long'); // false (too long)
Python
python
import re
username_regex = r'^[a-z0-9_-]{3,16}$'
bool(re.match(username_regex, 'john_doe')) # True
bool(re.match(username_regex, 'user123')) # True
bool(re.match(username_regex, 'ab')) # False (too short)
Go
go
usernameRegex := regexp.MustCompile(`^[a-z0-9_-]{3,16}$`)
usernameRegex.MatchString("john_doe") // true
usernameRegex.MatchString("user123") // true
usernameRegex.MatchString("ab") // false (too short)
Limitations
- Case sensitivity: Only allows lowercase letters (use
[a-zA-Z0-9_-]for case-insensitive) - No Unicode support: Doesn't support international characters
- Fixed length: Hard-coded 3-16 character limit
- No reserved words check: Doesn't prevent reserved usernames like
admin,root - No consecutive special chars: Allows
user__nameanduser--namewhich might be undesirable
When to Use
- Simple username validation for web applications
- When you want lowercase-only usernames
- Quick client-side validation
- Basic authentication systems
For production, consider:
- Case-insensitive matching:
^[a-zA-Z0-9_-]{3,16}$ - Checking against reserved username lists
- Preventing consecutive special characters
- Adding server-side validation
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!