Last updated
What Does the Username Validator Check?
The Username Validator checks usernames against configurable rules: length, allowed characters, format requirements, reserved words, and profanity filtering. It provides specific, actionable error messages for each validation failure — essential for user registration systems.
Valid Username Examples
✓ john_doe — letters and underscore, valid length
✓ alice123 — letters and numbers
✓ dev-user — letters and hyphen
✓ user.name — letters and dot (if dots allowed)
✓ CoolCoder99 — mixed case, numbers
✓ x_ae_a_12 — valid special characters
Invalid Username Examples with Error Messages
✗ ab
Error: Too short. Minimum length is 3 characters. (current: 2)
✗ this_username_is_way_too_long_for_our_platform_rules
Error: Too long. Maximum length is 30 characters. (current: 52)
✗ john doe
Error: Invalid character: space. Only letters, numbers, underscores, and hyphens are allowed.
✗ user@name
Error: Invalid character: @. Only letters, numbers, underscores, and hyphens are allowed.
✗ _username
Error: Username cannot start with a special character.
✗ username_
Error: Username cannot end with a special character.
✗ user__name
Error: Consecutive underscores are not allowed.
✗ admin
Error: "admin" is a reserved username and cannot be used.
✗ 123user
Error: Username cannot start with a number.
Length Validation
Configuration:
Minimum length: 3
Maximum length: 30
Examples:
"ab" ✗ Too short (2 chars)
"abc" ✓ Minimum length met
"john_doe" ✓ Valid length (8 chars)
"averylongusernamethatexceeds" ✗ Too long (28 chars — check your limit)
"a" ✗ Too short (1 char)
Recommended limits by platform type:
Social media: 3–20 characters
Gaming: 3–16 characters
Enterprise: 4–30 characters
Email-style: 1–64 characters (local part)
Character Set Validation
Common allowed character sets:
Strict (alphanumeric + underscore):
Regex: ^[a-zA-Z0-9_]+$
Allowed: letters, digits, underscore
Examples: john_doe ✓, user123 ✓, john-doe ✗
Standard (alphanumeric + underscore + hyphen):
Regex: ^[a-zA-Z0-9_-]+$
Allowed: letters, digits, underscore, hyphen
Examples: john-doe ✓, user_123 ✓, user.name ✗
Permissive (alphanumeric + underscore + hyphen + dot):
Regex: ^[a-zA-Z0-9_.-]+$
Allowed: letters, digits, underscore, hyphen, dot
Examples: user.name ✓, john-doe ✓, user@name ✗
Format Rules
Rule: Cannot start or end with special characters
_username ✗ starts with underscore
username_ ✗ ends with underscore
-username ✗ starts with hyphen
.username ✗ starts with dot
username ✓ starts and ends with letter
Rule: No consecutive special characters
user__name ✗ double underscore
user--name ✗ double hyphen
user..name ✗ double dot
user_-name ✗ mixed consecutive specials
user_name ✓ single underscore
Rule: Must start with a letter (strict mode)
123user ✗ starts with digit
_user ✗ starts with underscore
user123 ✓ starts with letter
Reserved Word Checking
Reserved usernames (cannot be registered):
System accounts: admin, root, system, daemon, nobody
Support: support, help, helpdesk, contact
Official: official, staff, moderator, mod
Platform: api, www, mail, ftp, smtp, pop3
Security: security, abuse, postmaster, webmaster
Generic: test, demo, example, sample, guest
Examples:
"admin" ✗ Reserved: system account name
"support" ✗ Reserved: may be confused with official support
"john_admin" ✓ Not reserved (contains "admin" but is not exactly "admin")
"administrator" ✓ Not in reserved list (check your config)
Pattern-based reserved words:
/^admin/i — blocks admin, Admin, ADMIN, administrator
/^support/i — blocks support, Support, support_team
Case Sensitivity Handling
Case-insensitive platform (most common):
"JohnDoe" and "johndoe" are the SAME username
Normalize to lowercase before uniqueness check
Registration: JohnDoe → stored as johndoe
Login attempt: JOHNDOE → normalized to johndoe → match ✓
Case-sensitive platform (rare):
"JohnDoe" and "johndoe" are DIFFERENT usernames
Both can be registered simultaneously
Recommendation: Use case-insensitive usernames to prevent
user confusion and impersonation attempts.
Profanity and Inappropriate Content Filtering
Basic word list check:
"badword123" ✗ Contains blocked word
"user_bad" ✗ Contains blocked word
"normaluser" ✓ No blocked content
Obfuscation detection:
"b4dw0rd" ✗ Leet-speak variant detected (4→a, 0→o)
"b.a.d.w.o.r.d" ✗ Dot-separated variant detected
"badw_ord" ✗ Underscore-separated variant detected
Custom word lists:
Add platform-specific terms to block
Brand names, competitor names, offensive terms
Update regularly as new patterns emerge
Uniqueness Checking
Real-time availability check during registration:
Input: "john_doe"
Checking availability...
Exact match: john_doe — TAKEN ✗
Case variants: John_Doe — TAKEN ✗
Similar usernames:
john_doe2 — Available ✓
john_doe_1 — Available ✓
johndoe — Available ✓
Suggestion: Try "john_doe2" or "johndoe"
Implementing Username Validation in Code
// JavaScript — comprehensive username validator
function validateUsername(username, options = {}) {
const {
minLength = 3,
maxLength = 30,
allowedChars = /^[a-zA-Z0-9_-]+$/,
reserved = ['admin', 'root', 'system', 'support'],
mustStartWithLetter = true
} = options;
const errors = [];
if (username.length < minLength)
errors.push(`Too short. Minimum ${minLength} characters.`);
if (username.length > maxLength)
errors.push(`Too long. Maximum ${maxLength} characters.`);
if (!allowedChars.test(username))
errors.push('Contains invalid characters.');
if (/[_-]$|^[_-]/.test(username))
errors.push('Cannot start or end with special characters.');
if (/[_-]{2}/.test(username))
errors.push('No consecutive special characters.');
if (mustStartWithLetter && /^\d/.test(username))
errors.push('Must start with a letter.');
if (reserved.includes(username.toLowerCase()))
errors.push(`"${username}" is a reserved username.`);
return { valid: errors.length === 0, errors };
}
validateUsername('admin');
// { valid: false, errors: ['"admin" is a reserved username.'] }
validateUsername('john_doe');
// { valid: true, errors: [] }
Common Use Cases
- User registration forms — validate before account creation
- Username change requests — re-validate against current rules
- Data migration — audit existing usernames for policy compliance
- API endpoints — validate username parameters in requests
- Admin tools — bulk-check usernames in user databases