0

This is another question about a previous question I had asked yesterday. I want user to be allowed to type US phone numbers in the following formats.

(800)-555-1212

800-555-1212

and have it only check the database for numbers 8005551212

I've seen that regex like /^[\+]?([0-9]*)\s*\(?\s*([0-9]{3})?\s*\)?[\s\-\.]*([0-9]{3})[\s\-\.]*([0-9]{4})[a-zA-Z\s\,\.]*[x\#]*[a-zA-Z\.\s]*([\d]*)/

may work but I'm not certain how to implement it into the code from the link I provided

I'm new to php and know nothing about regex. Any help is appreciated.

Community
  • 1
  • 1
Ben P. Dorsi-Todaro
  • 221
  • 1
  • 6
  • 20
  • Two things I will never bother validating by format, emails and phone numbers. If it's imperative that the user be contactable by phone, they'll get their number right – Phil Aug 08 '13 at 07:05
  • There are many answers.Check these links http://stackoverflow.com/questions/3357675/validating-us-phone-number-with-php-regex http://stackoverflow.com/questions/11705507/php-validation-of-us-phone-numbers http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation – 웃웃웃웃웃 Aug 08 '13 at 07:05
  • 2
    why this question is tagged with mysql and mysqli? – Your Common Sense Aug 08 '13 at 07:08
  • @YourCommonSense I tagged it with mysql and mysqli because it was part of a project that uses mysql I posted the link in the question. – Ben P. Dorsi-Todaro Aug 08 '13 at 07:27
  • @BenP.Dorsi-Todaro But it hasn't anything to do with the question. Keep in mind, that those tags are for the context of the question. – dan-lee Aug 08 '13 at 07:30
  • Well if it would be for the movie tickets project you'd tag it with 'tickets' and 'movie'? – Your Common Sense Aug 08 '13 at 07:32

5 Answers5

11

This function validate a phone number, return true if it validate and false if invalid. This function very simple i was wrote to.

    /**
     * @param $number
     *
     * @return bool
     */
    function validatePhoneNumber($number) {
        $formats = [
            '###-###-####', '####-###-###',
            '(###) ###-###', '####-####-####',
            '##-###-####-####', '####-####', '###-###-###',
            '#####-###-###', '##########', '#########',
            '# ### #####', '#-### #####'
        ];

        return in_array(
            trim(preg_replace('/[0-9]/', '#', $number)),
            $formats
        );
    }
0

if javascript is ok can go with

<script type="text/javascript">
function matchClick() {
  var re = new RegExp("Your regex here");
  if (document.formname.phone.value.match(re)) {
     alert("Ok");
     return true;
  } else {
     alert("Not ok");
     return false;
  }
} 
</script>

call this function onsubmit of form or onblur of textbox

If you have doubt about your regex you can validate it at http://www.regular-expressions.info/javascriptexample.html

user2408578
  • 464
  • 1
  • 4
  • 13
0

You can use this pattern

\(?\d{3,3}\)?-\d{3,3}-\d{4,4}

Taleh Ibrahimli
  • 750
  • 4
  • 13
  • 29
0

Try this,

<?php
    $t='/\(?[2-9][0-8][0-9]\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}/';
    $arr=preg_match($t,'(800)-555-1212',$mat);
    $arr=preg_match($t,'800-555-1212',$mat);
    print_r($mat);
?>

Tested here

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
0

Re: Rohan Kumar's solution

<?php
$t='/\(?[2-9][0-8][0-9]\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}/';
$arr=preg_match($t,'(800)-555-1212',$mat);
$arr=preg_match($t,'800-555-1212',$mat);
print_r($mat);
?>

It does address the issue of fake phone numbers such as 800-123-2222. Real phone numbers have a first digit of at least "2". While the other solutions do the format correctly, they don't address the issue of people putting in phone numbers like 800-000-1234, which would be correct in the other solutions provided.

Hadderach
  • 33
  • 7