1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. GTA Network forum is now in archive mode.

Check if name is in roleplay format

Discussion in 'Tutorials/Documentations' started by nobody, Sep 21, 2016.

  1. nobody

    nobody Well-Known Member Donator Language Board Moderator Tester

    Messages:
    108
    Likes Received:
    111
    Joined:
    Aug 25, 2016
    Hello,
    I made this pretty simple code for checking if name is in correct format for roleplaying server

    Name format:

    What will work:
    Name_Surname

    What will NOT work:
    NaMe_Surname
    N_Surname
    Name_surname
    Name_Surname_TopKek
    Name Surname Alexander
    Name
    Guad Is Handsome

    Only a-z characters work for name. So no cyrillic letters or other kind of letters.

    Code:
    Code:
            public bool isRoleplayName(string name)
            {
                string pattern = "^([A-Z][a-z]+_[A-Z][a-z]+)$";
                return System.Text.RegularExpressions.Regex.IsMatch(name, pattern); 
            }
    Function returns true if name is roleplay.
    Returns false if name is not roleplay

    Alternative version for accepting "Username Surname", without underscore.

    Name format:

    What will work:
    Name Surname

    What will NOT work:
    NaMe_Surname
    N_Surname
    Name_Surname
    Name_Surname_TopKek
    Name Surname Pepe
    Name
    Adam Not So Much

    Code:

    Code:
    public bool isRoleplayName(string name)
            {
                string pattern = "^([A-Z][a-z]+ [A-Z][a-z]+)$";
                return System.Text.RegularExpressions.Regex.IsMatch(name, pattern); 
            }

    If you want to allow space or underscore between name, you can use this code:

    (Banou suggestion)
    Code:
    public bool isRoleplayName(string name)
            {
                string pattern = "^([A-Z][a-z]+[ _][A-Z][a-z]+)$";
                return System.Text.RegularExpressions.Regex.IsMatch(name, pattern);  
            }

    Expresion for allowing 3 words in a name:
    For example:

    Leonardo da Vinci
    Guad van Hacker
    Tom Antanas
    Leonardas Di Capris
    Code:
    public bool isRoleplayName(string name)
            {
                string pattern = "^(([A-Z][a-z]+)(([ _][A-Z][a-z]+)|([ _][A-z]+[ _][A-Z][a-z]+)))$";
                return System.Text.RegularExpressions.Regex.IsMatch(name, pattern);   
            }
     
    Last edited: Sep 23, 2016
    Theory, Zuthrex, Lowi and 2 others like this.
  2. MKB

    MKB Member

    Messages:
    37
    Likes Received:
    6
    Joined:
    Aug 10, 2016
    Very nice, but, just want to note that there are legitimate names with more than two 'parts' to them. A lot of Spanish, Mexican and some French surnames are two-part. Back on SA:MP, this was generally remedied by simply removing the space between the two parts and keeping the capitol letter, but I see that your system does not support this (as far as I can see).
     
  3. nobody

    nobody Well-Known Member Donator Language Board Moderator Tester

    Messages:
    108
    Likes Received:
    111
    Joined:
    Aug 25, 2016
    Show me an example of the name and i will add it :=)
     
  4. MKB

    MKB Member

    Messages:
    37
    Likes Received:
    6
    Joined:
    Aug 10, 2016
    Michael DeSanta
    Hulio De La Rosa
    etc.
     
  5. nobody

    nobody Well-Known Member Donator Language Board Moderator Tester

    Messages:
    108
    Likes Received:
    111
    Joined:
    Aug 25, 2016
    Added new expression for allowing 3 words. :=)
     
  6. Vincent

    Vincent Member

    Messages:
    15
    Likes Received:
    5
    Joined:
    Sep 23, 2016
    Very nice :=)
     
  7. JayronTaylor

    JayronTaylor Active Member

    Messages:
    65
    Likes Received:
    13
    Joined:
    Dec 26, 2016
  8. JakeKaneisback

    JakeKaneisback Member

    Messages:
    8
    Likes Received:
    0
    Joined:
    Aug 17, 2016
    How do you even change your name on GTANET
     
  9. MissMelissa

    MissMelissa New Member

    Messages:
    0
    Likes Received:
    0
    Joined:
    Dec 26, 2016
    What a bump! In the settings.
     
  10. JakeKaneisback

    JakeKaneisback Member

    Messages:
    8
    Likes Received:
    0
    Joined:
    Aug 17, 2016
    it's a lovely bump. thanks for answer though
     
  11. WarrioR™

    WarrioR™ Member

    Messages:
    10
    Likes Received:
    12
    Joined:
    Apr 10, 2017
    You can also:
    Code:
    public Script(){API.onPlayerConnected += API_onPlayerConnected;}
    And :
    Code:
         private void API_onPlayerConnected(Client player)
         {
         string cara = "^(([A-Z][a-z]+)(([ ][A-Z][a-z]+)|([ ][A-z]+[ _][A-Z][a-z]+)))$";
         var pseudo = System.Text.RegularExpressions.Regex.IsMatch(player.name, cara);
         if(pseudo) { }
         else{API.sendChatMessageToAll("Pseudo incorrect, exemple : John_Smith);}
     
  12. MissMelissa

    MissMelissa New Member

    Messages:
    0
    Likes Received:
    0
    Joined:
    Dec 26, 2016
    Uh, why not just check for !pseudo instead of that useless else? Also, I assume you want to send the message to only the player connecting, not to all:
    Code:
    if(!pseudo) { API.sendChatMessage(player, "Pseudo incorrect"); }
    You could also rewrite that regex as: (as in, you don't need the groups if you're just going to check if it matches)
    Code:
    ^[A-Z][a-z]+[ _][A-Z][a-z]+$
     

Share This Page