Skip to content

Instantly share code, notes, and snippets.

@RTAndrew
Last active July 14, 2023 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RTAndrew/a9d26f70f05eca7abcf77ef5bc612fd7 to your computer and use it in GitHub Desktop.
Save RTAndrew/a9d26f70f05eca7abcf77ef5bc612fd7 to your computer and use it in GitHub Desktop.
Validate the new Discord username
/**
* 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 };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment