Form validation - Allow numbers and some special characters

Discussion in 'PHP' started by webbyready, Dec 29, 2011.

  1. #1
    Hello All,

    I have the following code that is used in my form processing script which only allows for "numbers".

    if(!is_numeric($_REQUEST['phone'])){$errors[]="Phone number may only contain numbers";}
    Code (markup):
    I would like to also allow some special characters. The characters would be as follows (without the quotation marks; second character is a space)
    "-"
    " "
    "("
    ")"
    "_"
    "."
    "+"
    "="
    "x"
    "ext"
    "extension"

    Any help would be greatly appreciated!!

    Thanks
     
    webbyready, Dec 29, 2011 IP
  2. stats

    stats Well-Known Member

    Messages:
    586
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #2
    what does "=" and "_" have to do with a phone number ? :)

    as per the rest, i think you gonna have either a problem or a headache with the "ext" and "extension" since they are letters and you want to NOT allow any letter but only these combinations of letters .. it's kinda oakward

    what you can do instead, is provide another input field in your form for the extension itself

    as per the rest .. preg_replace('/[^()- .+=.]/', '', $phone);
    i don't remember those special chars for regex, but if some of these give problem - try escaping them (something like \. instead of just .) , and if there's a problem with the space - try \s instead

    welcome to the woodoo world of regex and good luck (you'll definitely need some luck there) :)
     
    stats, Dec 29, 2011 IP