/** * Validate Discord username according to the services's rules * @param username the user's username * @returns ```{ hasSuccessiveDots, isCaseSensitive, containsSpecialChars, breaksMinMaxLength }``` * @summary It does not support the previews version: discordUSerName#1234 * @see https://support.discord.com/hc/en-us/articles/12620128861463-New-Usernames-Display-Names */ export const validateDiscordUsername = (username: string) => { // The RegEx support this validation: // ^(?!.*[.]{2,})[a-zA-Z0-9_.]{2,32}$ let hasSuccessiveDots = false; let isCaseSensitive = false; let containsSpecialChars = false; let breaksMinMaxLength = false; const _hasSuccessiveDots = new RegExp('^.*[.]{2,}$').test(username); const _isCaseSensitive = new RegExp('[A-Z]+').test(username); const _containsSpecialChars = new RegExp('[^A-Za-z0-9_.]').test(username); if (username.length < 2 || username.length > 32) breaksMinMaxLength = true; if (_hasSuccessiveDots) hasSuccessiveDots = true; if (_isCaseSensitive) isCaseSensitive = true; if (_containsSpecialChars) containsSpecialChars = true; return { hasSuccessiveDots, isCaseSensitive, containsSpecialChars, breaksMinMaxLength }; };