Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    rix
    rix is offline
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Regex validation of a surname

    Now this is a bit of a sore point for me, seeing as I'm an experienced Java programmer but I'm completely useless at regex. Now I'm supposed to write a javascript validation for a surname that only allowes letters, dash (-), space ( ) and the apostrophe ('). The dash, space or apostrophe is also not allowed to be the first or last characters. Here is some valid surnames and patterns:
    O'Connor
    MacDonald
    Smith-Henderson
    van der Merwe
    Hill

    Here is some of the regex's that I've tried:
    /^[a-zA-Z\-\']+$
    ^[a-zA-Z]+[a-zA-Z ]*$
    ^([a-zA-Z]+)$

    Anyone that can help me with this ? It would be greatly appreciated!

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Try this:
    Code:
    if(mystring.match(/^[ |'|-]/)||!(/^[a-zA-Z- ']*$/.test(mystring))){
    alert('Not a valid name')
    }
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    rix
    rix is offline
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Kor, you are a genius and a savior!
    Thanks so much

  • #4
    rix
    rix is offline
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Right, altho Kor definitely put me on the right track, the inevitable happened - they added more specs to it :/

    Now, what i've got at the moment is this:
    function checkValidChars(pMyValue)
    {
    if(pMyValue.match(/^[ |'|-]/)||!(/^[a-zA-Z][a-zA-Z- ']*[a-zA-Z]$/.test(pMyValue))){
    return false;
    }
    return true;
    }
    This is the old spec:
    Now I'm supposed to write a javascript validation for a surname that only allowes letters, dash (-), space ( ) and the apostrophe ('). The dash, space or apostrophe is also not allowed to be the first or last characters. Here is some valid surnames and patterns:
    O'Connor
    MacDonald
    Smith-Henderson
    van der Merwe
    Hill
    Now the added on parts are:
    -The input is also allowed to be only one character (and that excludes the -, ' and space).
    -Check that there's only one ', - or space
    -The ', - and space is not allowed to be the first and last character.

    Am I asking for too much ?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •