Skip to content

Instantly share code, notes, and snippets.

@Laiteux
Last active June 17, 2025 09:11
Show Gist options
  • Save Laiteux/bc6895e8e3dcfe7ffaa91cf2c1077031 to your computer and use it in GitHub Desktop.
Save Laiteux/bc6895e8e3dcfe7ffaa91cf2c1077031 to your computer and use it in GitHub Desktop.
C# method to validate a Telegram username using RegEx
using System;
using System.Text.RegularExpressions;
bool ValidateTelegramUsername(string username)
=> Regex.IsMatch(username.TrimStart('@'),
"^(?=.{4,32}$)(?!.*__)(?!^(telegram|admin|support))[a-z][a-z0-9_]*[a-z0-9]$",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
Console.WriteLine(ValidateTelegramUsername("Laiteux1")); // True
Console.WriteLine(ValidateTelegramUsername("1Laiteux")); // False: Must start with a letter
Console.WriteLine(ValidateTelegramUsername("Lai")); // False: Too short (must contain between 4 and 32 characters)
Console.WriteLine(ValidateTelegramUsername("_Laiteux_")); // False: Starts or ends with an underscore
Console.WriteLine(ValidateTelegramUsername("Lait__eux")); // False: Contains two or more consecutive underscores
Console.WriteLine(ValidateTelegramUsername("AdminLaiteux")); // False: Starts with "Telegram", "Admin" or "Support"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment