Jump to content

Regular expression for phone numbers:


terungwa

Recommended Posts

Regular expression for phone numbers with country code:

I am trying to get the regular expression for phone number with this format: 0802-810-4000.

I have come up with this regex

if(!preg_match(@"^\(?([0-9]{4})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", $_POST["phone_number"]))
    {
        // set error for phone_number field
        $reg_errors['phone_number'] = '<p class="warning">Phone must comply with this format: 0803-333-4000</p>';
    }

When i post data via my form using this phone number entry: 0802-810-4000 i get my error variable returned.

What is the right regex format to use:

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/285780-regular-expression-for-phone-numbers/
Share on other sites

Did you copy that expression from some C# code? Tsk tsk.

 

The preg_* functions need delimiters around the expression. Delimiters such as /s.

if(!preg_match("/^\(?([0-9]{4})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/", $_POST["phone_number"]))

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.