Username Validation Regex

Validate usernames with 3-16 alphanumeric characters, underscores, and hyphens.

Pattern & Test String

About This Pattern

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

Flags: g

Results

Enter a regex pattern to see the results here.

Frequently Asked Questions

What characters are allowed in this username pattern?

This pattern allows letters (a-z, A-Z), numbers (0-9), underscores (_), and hyphens (-). The username must be 3-16 characters long.

Can usernames start or end with special characters?

Yes, with this pattern. To prevent starting/ending with underscores or hyphens, use: ^[a-zA-Z0-9]([a-zA-Z0-9_-]{1,14})[a-zA-Z0-9]$

How do I change the length requirements?

Modify {3,16} to your desired range. For example, {5,20} requires 5-20 characters.

Practical Examples

Registration Form

Validate username input during user account creation.

Profile URL Generation

Ensure usernames are URL-safe for profile pages.

Username Availability Check

Validate format before checking database availability.

Common Issues & Solutions

Consider case-insensitive matching if usernames should be unique regardless of case. Store lowercase versions for comparison.

This pattern allows consecutive underscores/hyphens. Add logic to prevent patterns like 'user____name' if desired.

Reserved words (like 'admin', 'root') should be checked separately from regex validation.