PHP Mailer Script Help - Need a quick line or 2 to allow only Numbers...

Discussion in 'PHP' started by studlee46, Jun 6, 2011.

  1. #1
    I have a simple php mailer script that I've been using. It's been pretty good at blocking spam posts, but I'm getting more and more spam. So I need another line of code or 2 to block letters and only allow Numbers + this character "-" for the Phone number input. Currently, I have these lines of code blocking other forms of form spam:

    if(strpos($_POST['comments'], 'http://') !== false) die();
    if(strpos($_POST['comments'], '<a') !== false) die();
    if(strpos($_POST['phone'], 'http://') !== false) die();
    if(strpos($_POST['phone'], '<a') !== false) die();

    So this needs to be able to submit:

    555-555-5555;

    But if it contains any letter, it needs to die.

    I appreciate any help. If it gets too complex, I'm not opposed to paying someone.

    Thanks!
     
    studlee46, Jun 6, 2011 IP
  2. dazst

    dazst Active Member

    Messages:
    115
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    78
    #2
    if(!preg_match("|^[0-9-]+|",$_POST['phone'])) die();
     
    dazst, Jun 6, 2011 IP
  3. studlee46

    studlee46 Peon

    Messages:
    102
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks a ton dazst! Much appreciated!
     
    studlee46, Jun 6, 2011 IP
  4. RootShell

    RootShell Well-Known Member

    Messages:
    855
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    130
    Digital Goods:
    1
    #4
    Or you can use the php build in function is_numeric :
    if(!is_numeric($something))die('Not Numeric');
    Code (markup):
    ;)
     
    RootShell, Jun 9, 2011 IP
  5. pehpeh

    pehpeh Guest

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you useful information
     
    pehpeh, Jun 10, 2011 IP
  6. The Webby

    The Webby Peon

    Messages:
    1,852
    Likes Received:
    30
    Best Answers:
    1
    Trophy Points:
    0
    #6
    The problem with is_numeric() is that 0X0046ffcc or 2.43578365+E18 are valid numbers as well. Sometimes you dont want that. In that scenario preg_match is the best way to go..
     
    The Webby, Jun 12, 2011 IP
  7. MezaTech

    MezaTech Greenhorn

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #7
    if(!preg_match("|^[0-9-]+|",$_POST['phone'])) die();

    it's the best
     
    MezaTech, Jun 19, 2011 IP