1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need help with regular expressions

Discussion in 'PHP' started by khan11, Feb 15, 2011.

  1. #1
    Hello everyone!

    I need some help with regular expressions. I'm using preg_match to validate name field for a form. I need to allow only alphabets and spaces. I'm unable to get it working for spaces. Please suggest me some code.

    Here is what i'm doing. if(!preg_match('[a-zA-Z\s]', $name))...

    Thanks
     
    khan11, Feb 15, 2011 IP
  2. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #2
    Right now you're letting in just a single character, you need to wrap your expression in () and add + after the [] which means 1 or more.

    This should work: ([a-zA-Z\s]+)
     
    crazyryan, Feb 15, 2011 IP
  3. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Thanks. But it's now letting everything pass through. Including numbers, special characters etc.. :-(
     
    khan11, Feb 15, 2011 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    Note the space after the second Z, thats all you have to do.
    
    "/^[a-zA-Z ]+$/"
    
    PHP:
    ..and this one indicates the first letter needs to be a Capital letter, which names should be I guess.
    
    "/^[A-Z][a-zA-Z ]+$/"
    
    PHP:
     
    MyVodaFone, Feb 15, 2011 IP
    khan11 likes this.
  5. khan11

    khan11 Active Member

    Messages:
    615
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #5
    this worked fine for me. Thank you!
     
    khan11, Feb 15, 2011 IP
  6. way2tech

    way2tech Member

    Messages:
    245
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #6
    hi,
    this is work properly..
    Thanks..
     
    way2tech, Feb 16, 2011 IP