Jump to content

Regular expression for phone numbers:


terungwa
Go to solution Solved by requinix,

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
Share on other sites

  • Solution

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"]))
Edited by requinix
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.