SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: regexp help

  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2005
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regexp help

    HI

    I am looking for a regexp for a surname field

    I got something like this

    if(!eregi("^[a-z'-]*$", trim($_POST['surname']))){blah blah

    Now valid surnames are shown below

    Johnson
    Johnson's
    John-johnson

    etc etc

    But my code above gives an error for all my valids.

    Please Help
    Niva

  2. #2
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All those surnames pass the regex you gave ok. Are you sure you not getting confused because of the !eregi ?
    i.e you're code is doing:
    PHP Code:
    if(!eregi("^[a-z'-]*$"trim($_POST['surname']))){
       echo 
    'invalid';
    } else {
       echo 
    'valid';


  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2005
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks

    It works now, you're right I confused the logic, how would I add in a "(double quotes) to be accepted as well..


    Thanks
    Niva

  4. #4
    SitePoint Guru momos's Avatar
    Join Date
    Apr 2004
    Location
    Belgium
    Posts
    919
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just wrote this by hard, so I don't know if it works: "^[a-z]+((\-|\')[a-z]+)*$", but you should at least escape things like: "-"

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the correct regexp should be something like

    /^[a-z]+([ '-][a-z]+)*$/i

    further improvement would be supporting accented chars

  6. #6
    SitePoint Evangelist
    Join Date
    Mar 2005
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks

    What about the double quotes..."


Bookmarks

Posting Permissions

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