Skip to content

Instantly share code, notes, and snippets.

@RTAndrew
Last active July 14, 2023 12:42
Show Gist options
  • Select an option

  • Save RTAndrew/a9d26f70f05eca7abcf77ef5bc612fd7 to your computer and use it in GitHub Desktop.

Select an option

Save RTAndrew/a9d26f70f05eca7abcf77ef5bc612fd7 to your computer and use it in GitHub Desktop.

Revisions

  1. RTAndrew revised this gist Jul 14, 2023. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions validate-discord-username.ts
    Original file line number Diff line number Diff line change
    @@ -19,11 +19,8 @@ export const validateDiscordUsername = (username: string) => {
    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 };
  2. RTAndrew renamed this gist Jul 14, 2023. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. RTAndrew created this gist Jul 14, 2023.
    30 changes: 30 additions & 0 deletions validate-discord-username
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    /**
    * 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 };
    };