Regular expression for first and last name

后端 未结 24 1913
温柔的废话
温柔的废话 2020-11-22 10:03

For website validation purposes, I need first name and last name validation.

For the first name, it should only contain letters, can be several words with spaces, an

相关标签:
24条回答
  • 2020-11-22 10:14

    I have searched and searched and played and played with it and although it is not perfect it may help others making the attempt to validate first and last names that have been provided as one variable.

    In my case, that variable is $name.

    I used the following code for my PHP:

        if (preg_match('/\b([A-Z]{1}[a-z]{1,30}[- ]{0,1}|[A-Z]{1}[- \']{1}[A-Z]{0,1}  
        [a-z]{1,30}[- ]{0,1}|[a-z]{1,2}[ -\']{1}[A-Z]{1}[a-z]{1,30}){2,5}/', $name)  
        # there is no space line break between in the above "if statement", any that   
        # you notice or perceive are only there for formatting purposes.  
        # 
        # pass - successful match - do something
        } else {
        # fail - unsuccessful match - do something
    

    I am learning RegEx myself but I do have the explanation for the code as provided by RegEx buddy.
    Here it is:

    Assert position at a word boundary «\b»

    Match the regular expression below and capture its match into backreference number 1
    «([A-Z]{1}[a-z]{1,30}[- ]{0,1}|[A-Z]{1}[- \']{1}[A-Z]{0,1}[a-z]{1,30}[- ]{0,1}|[a-z]{1,2}[ -\']{1}[A-Z]{1}[a-z]{1,30}){2,5}»

    Between 2 and 5 times, as many times as possible, giving back as needed (greedy) «{2,5}»

    * I NEED SOME HELP HERE WITH UNDERSTANDING THE RAMIFICATIONS OF THIS NOTE *

    Note: I repeated the capturing group itself. The group will capture only the last iteration. Put a capturing group around the repeated group to capture all iterations. «{2,5}»

    Match either the regular expression below (attempting the next alternative only if this one fails) «[A-Z]{1}[a-z]{1,30}[- ]{0,1}»

    Match a single character in the range between “A” and “Z” «[A-Z]{1}»

    Exactly 1 times «{1}»

    Match a single character in the range between “a” and “z” «[a-z]{1,30}»

    Between one and 30 times, as many times as possible, giving back as needed (greedy) «{1,30}»

    Match a single character present in the list “- ” «[- ]{0,1}»

    Between zero and one times, as many times as possible, giving back as needed (greedy) «{0,1}»

    Or match regular expression number 2 below (attempting the next alternative only if this one fails) «[A-Z]{1}[- \']{1}[A-Z]{0,1}[a-z]{1,30}[- ]{0,1}»

    Match a single character in the range between “A” and “Z” «[A-Z]{1}»

    Exactly 1 times «{1}»

    Match a single character present in the list below «[- \']{1}»

    Exactly 1 times «{1}»

    One of the characters “- ” «- » A ' character «\'»

    Match a single character in the range between “A” and “Z” «[A-Z]{0,1}»

    Between zero and one times, as many times as possible, giving back as needed (greedy) «{0,1}»

    Match a single character in the range between “a” and “z” «[a-z]{1,30}»

    Between one and 30 times, as many times as possible, giving back as needed (greedy) «{1,30}»

    Match a single character present in the list “- ” «[- ]{0,1}»

    Between zero and one times, as many times as possible, giving back as needed (greedy) «{0,1}»

    Or match regular expression number 3 below (the entire group fails if this one fails to match) «[a-z]{1,2}[ -\']{1}[A-Z]{1}[a-z]{1,30}»

    Match a single character in the range between “a” and “z” «[a-z]{1,2}»

    Between one and 2 times, as many times as possible, giving back as needed (greedy) «{1,2}»

    Match a single character in the range between “ ” and “'” «[ -\']{1}»

    Exactly 1 times «{1}»

    Match a single character in the range between “A” and “Z” «[A-Z]{1}»

    Exactly 1 times «{1}»

    Match a single character in the range between “a” and “z” «[a-z]{1,30}»

    Between one and 30 times, as many times as possible, giving back as needed (greedy) «{1,30}»

    I know this validation totally assumes that every person filling out the form has a western name and that may eliminates the vast majority of folks in the world. However, I feel like this is a step in the proper direction. Perhaps this regular expression is too basic for the gurus to address simplistically or maybe there is some other reason that I was unable to find the above code in my searches. I spent way too long trying to figure this bit out, you will probably notice just how foggy my mind is on all this if you look at my test names below.

    I tested the code on the following names and the results are in parentheses to the right of each name.

    1. STEVE SMITH (fail)
    2. Stev3 Smith (fail)
    3. STeve Smith (fail)
    4. Steve SMith (fail)
    5. Steve Sm1th (passed on the Steve Sm)
    6. d'Are to Beaware (passed on the Are to Beaware)
    7. Jo Blow (passed)
    8. Hyoung Kyoung Wu (passed)
    9. Mike O'Neal (passed)
    10. Steve Johnson-Smith (passed)
    11. Jozef-Schmozev Hiemdel (passed)
    12. O Henry Smith (passed)
    13. Mathais d'Arras (passed)
    14. Martin Luther King Jr (passed)
    15. Downtown-James Brown (passed)
    16. Darren McCarty (passed)
    17. George De FunkMaster (passed)
    18. Kurtis B-Ball Basketball (passed)
    19. Ahmad el Jeffe (passed)

    If you have basic names, there must be more than one up to five for the above code to work, that are similar to those that I used during testing, this code might be for you.

    If you have any improvements, please let me know. I am just in the early stages (first few months of figuring out RegEx.

    Thanks and good luck, Steve

    0 讨论(0)
  • 2020-11-22 10:14
    ^\p{L}{2,}$
    

    ^ asserts position at start of a line.

    \p{L} matches any kind of letter from any language

    {2,} Quantifier — Matches between 2 and unlimited times, as many times as possible, giving back as needed (greedy)

    $ asserts position at the end of a line

    So it should be a name in any language containing at least 2 letters(or symbols) without numbers or other characters.

    0 讨论(0)
  • 2020-11-22 10:15

    Don't forget about names like:

    • Mathias d'Arras
    • Martin Luther King, Jr.
    • Hector Sausage-Hausen

    This should do the trick for most things:

    /^[a-z ,.'-]+$/i

    OR Support international names with super sweet unicode:

    /^[a-zA-ZàáâäãåąčćęèéêëėįìíîïłńòóôöõøùúûüųūÿýżźñçčšžÀÁÂÄÃÅĄĆČĖĘÈÉÊËÌÍÎÏĮŁŃÒÓÔÖÕØÙÚÛÜŲŪŸÝŻŹÑßÇŒÆČŠŽ∂ð ,.'-]+$/u

    0 讨论(0)
  • 2020-11-22 10:15

    Simplest way. Just check almost 2 words.

    /^[^\s]+( [^\s]+)+$/
    

    Valid names

    • John Doe
    • pedro alberto ch
    • Ar. Gen
    • Mathias d'Arras
    • Martin Luther King, Jr.

    No valid names

    • John
    • 陳大文
    0 讨论(0)
  • 2020-11-22 10:15

    So, with customer we create this crazy regex:

    (^$)|(^([^\-!#\$%&\(\)\*,\./:;\?@\[\\\]_\{\|\}¨ˇ“”€\+<=>§°\d\s¤®™©]| )+$)
    
    0 讨论(0)
  • 2020-11-22 10:16

    I use:

    /^(?:[\u00c0-\u01ffa-zA-Z'-]){2,}(?:\s[\u00c0-\u01ffa-zA-Z'-]{2,})+$/i
    

    And test for maxlength using some other means

    0 讨论(0)
提交回复
热议问题