terungwa Posted January 29, 2014 Share Posted January 29, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/285780-regular-expression-for-phone-numbers/ Share on other sites More sharing options...
Solution requinix Posted January 29, 2014 Solution Share Posted January 29, 2014 (edited) 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 January 29, 2014 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/285780-regular-expression-for-phone-numbers/#findComment-1467003 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.