Username Validation

Validate username format with letters, numbers, and underscores. Perfect for user registration systems.

BeginnerIdentity
Regular Expression Pattern
^[a-zA-Z0-9_]20$

Pattern Breakdown:

  • ^ - Start of string
  • [a-zA-Z0-9_] - Letters, numbers, or underscore
  • 20 - Between 3 and 20 characters
  • $ - End of string

Username Rules:

Length
Must be between 3 and 20 characters
Letters
Can contain uppercase and lowercase letters (a-z, A-Z)
Numbers
Can contain digits (0-9)
Underscore
Can contain underscores (_)
No Spaces
Spaces are not allowed
No Special Chars
Special characters (except underscore) are not allowed
Live Username Tester
Implementation Examples
// Username validation regex
const usernameRegex = /^[a-zA-Z0-9_]{3,20}$/;

function validateUsername(username) {
  return usernameRegex.test(username);
}

// Usage examples
console.log(validateUsername("user123")); // true
console.log(validateUsername("john_doe")); // true
console.log(validateUsername("admin_user")); // true
console.log(validateUsername("ab")); // false (too short)
console.log(validateUsername("user-name")); // false (contains hyphen)
Test Cases
"user123"

Valid username with letters and numbers

Valid
"john_doe"

Valid username with underscore

Valid
"admin_user"

Valid username with underscore

Valid
"TestUser"

Valid username with mixed case

Valid
"user_123_test"

Valid username with multiple underscores

Valid
"ab"

Too short (less than 3 characters)

Invalid
"this_username_is_too_long_to_be_valid"

Too long (more than 20 characters)

Invalid
"user-name"

Contains hyphen (not allowed)

Invalid
"user name"

Contains space (not allowed)

Invalid
"user@name"

Contains special character (not allowed)

Invalid

Share & Save

Share this regex pattern with others or save it to your favorites