Last active
June 17, 2025 09:11
-
-
Save Laiteux/bc6895e8e3dcfe7ffaa91cf2c1077031 to your computer and use it in GitHub Desktop.
C# method to validate a Telegram username using RegEx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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