Skip to main content
AllDevToolsHub
Back to all patterns

Twitter/X Handle Validation

Validation

Validates Twitter/X handles with optional @ prefix, 1-15 alphanumeric characters and underscores.

/^@?[A-Za-z0-9_]{1,15}$/

How it works

Twitter handles are 1-15 characters consisting of letters, numbers, and underscores. The @ prefix is optional in this pattern.

Test Cases

Should Match

  • @elonmusk
  • john_doe
  • User123

Should NOT Match

  • @this_handle_is_too_long_for_twitter
  • @has space
  • @has.dot

Quick Summary

Validates Twitter/X handles: optional @ prefix, 1-15 characters of letters, numbers, and underscores. Does not enforce Twitter additional rules (no consecutive underscores, no purely numeric handles).

Key Takeaways

Key Takeaways

  • Maximum 15 characters (Twitter limit)
  • Only letters, numbers, and underscores, no dots, hyphens, or spaces
  • The @ prefix is optional in this pattern
  • Twitter also disallows purely numeric handles and consecutive underscores
Use Cases

When to use it

  • Validating social media handle inputs in profile forms
  • Extracting @mentions from tweet text
  • Checking Twitter handle format before API calls
Watch out

Common Mistakes

  • Forgetting that Twitter handles are case-insensitive, normalize before comparison
  • Not stripping the @ before storing, decide on a consistent storage format
FAQ

Twitter/X Handle Validation, Frequently Asked

How do I extract all @mentions from a tweet?

Use the global flag: /@([A-Za-z0-9_]{1,15})/g to find all mentions in a string.

What is the maximum Twitter handle length?

15 characters (not counting the @). Display names can be up to 50 characters.