0

I want to validate a phone number input. The phone numbers should be mix of numeric number and some of the punctuation, like -,+ ,( and ).

The input don't have specific format, it can be

  • 014-455464564
  • +6054-4554
  • (+60)-4554

How can I do that in preg_match?

dev-jim
  • 2,404
  • 6
  • 35
  • 61
  • 1
    I guess it's not a duplicate, but here's an entry that may help you, http://stackoverflow.com/q/123559/527096 – potench Jul 04 '12 at 17:03

1 Answers1

0

I guess this one works based on your examples.

$pattern = '/^[0-9_\-\+\(\)\ ]+$/';

    $val = '014-455464564';
    if (preg_match($pattern, $val))
        echo $val.' TRUE<br />';
    else
        echo $val.' FALSE<br />';

    $val = '+6054-4554';
    if (preg_match($pattern, $val))
        echo $val.' TRUE<br />';
    else
        echo $val.' FALSE<br />';

    $val = '(+60)-4554';
    if (preg_match($pattern, $val))
        echo $val.' TRUE<br />';
    else
        echo $val.' FALSE<br />';

    $val = '(+60)-4554-sss';
    if (preg_match($pattern, $val))
        echo $val.' TRUE<br />';
    else
        echo $val.' FALSE<br />';

    $val = '(+60)-4 554';
    if (preg_match($pattern, $val))
        echo $val.' TRUE<br />';
    else
        echo $val.' FALSE<br />';

Also you may want to check http://regexlib.com

Mustafa
  • 825
  • 3
  • 14
  • 37