Allowing a DASH to pass as numeric

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moishy
    New Member
    • Oct 2006
    • 104

    Allowing a DASH to pass as numeric

    I want that certain fields in my form must be numeric.

    I'm using the "if_numeric " code for that.

    But if I submit a form with a phone number that contanes a dash, It will not be accepted.

    Example:

    Phone: 1516-678-9010

    Will not be accepted.

    However, only such a number will be accepted:

    Phone: 15166789010


    So, how to I add a "DASH" to the "Whitelist" ?
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    The following is a little snippet that, after removing the blanks and the dashes, checks if the input field contains 9 to 14 digits only. You could use that to check your telephone no, allowing people to have blanks or dashes inside.
    [php]
    $tests = array( "314-5565-400",
    "800-555-4400",
    "(314)555-4000",
    "3146784000 ",
    "555",
    "5342 875 6478",
    "1234-123-12345"
    );
    foreach ($tests as $test) {
    if (preg_match('^\ d{9,14}$^', str_replace(arr ay(" ","-"), "", $test))) {
    echo "Matched on $test<br />";
    }
    else {
    echo "Failed match on $test<br />";
    }
    }
    ?>
    [/php]

    Ronald :cool:

    Comment

    • netvision
      New Member
      • Oct 2006
      • 5

      #3
      The ascii character of '-' has to be taken and check the ascii value entered using Charcodeat function.

      Comment

      Working...