Forum Moderators: coopster

Message Too Old, No Replies

Validating phone number with extension

cannot make extension any digit number (0 up to 5)

         

henry0

2:28 pm on Nov 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This does well in both examples:
$t="222-222-2222-122"; or $t="222-222-2222;
if(! preg_match('/^\d{3}-\d{3}-\d{4}(\-\d{3})?$/', $t))
But I cannot find how to make the last part (phone extension) matching any optional number of digits from 1 to 5 digits
As is since it is optional it works if not any number are entered but if any are entered it indeed calls for 3 or whatever is required between {}
How may I modify it to accept either 0 or any digits from 1 to 5 any digits

d40sithui

4:28 pm on Nov 9, 2007 (gmt 0)

10+ Year Member



instead of:
if(! preg_match('/^\d{3}-\d{3}-\d{4}(\-\d{3})?$/', $t))

use:
if(! preg_match('/^\d{3}-\d{3}-\d{4}(\-\d{0,5})?$/', $t))

henry0

4:42 pm on Nov 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thanks,
I don't see yet in my future a day when I'll be proficient enough with regex :)

henry0

5:14 pm on Nov 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If anyone needs it
I made it with dashes and () for the extension instead of a single dash


$t="222-222-2222 (222)";
// if(! preg_match('/^\d{3}-\d{3}-\d{4}(\-\d{0,5})?$/', $t))

if(! preg_match('/^\d{3}-\d{3}-\d{4} (\(\d{0,5})\)?$/', $t))

henry0

7:02 pm on Nov 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry it had a glitch that I did not see at first test

the following allows for an optional extension after a space and needs to be between parentheses


Format:
$t="222-222-2221 (11)";

if(! preg_match('/^\d{3}-\d{3}-\d{4}(\ \(\d{0,5}\))?$/', $t) )

d40sithui

7:19 pm on Nov 9, 2007 (gmt 0)

10+ Year Member



gnarly