Welcome to WebmasterWorld Guest from

Forum Moderators: keyplyr & mack

Message Too Old, No Replies

Sensible Regex

This works, but is it a good idea?

3:48 am on Dec 17, 2009 (gmt 0)

Junior Member

5+ Year Member

joined:Mar 26, 2008
votes: 0

I am working my way through yet another textbook. I am almost getting regex. But not quite.

Anyway, here is a possible regex for a US phone number. It works, I tried it. But is this kind of redundancy a good plan, or is it going too far? I notice most professional web sites don't bother.


9:49 am on Dec 17, 2009 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member piatkow is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Apr 5, 2006
votes: 44

Depends what you are going to do with the phone number. If it is just to read off the screen as you dial then why antagonise your customers with a rigid format. If you will need to capture it for caller id then that's another matter.

What really winds me up is overseas sites that insist on a phone number but won't let me enter it in international format with a leading plus sign.

8:37 pm on Dec 17, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
votes: 0


Yeah this is kind of limiting, and it looks like you're saying "3 digits or (3 digits)" . . . the international consideration is important too. See below for a different way to make "(" and ")" optional.

Here's one I used for a recent project. It also doesn't allow the leading +, because designed for U.S. only, but allows a wider range and passes several formats. The notes below say how to make the dashes/separators optional and add the optional leading +. If you have/know how to run perl, you can just test it.

@phones = (
'02 9988 1288',
'(02) 9988 1288',
'02.9988 1288',
'123 456 1288',
'(123 ) 456 1288',
$reg = '\s*\(*\d{2,3}\s*\)*[\-\.\s]+?\d{3,4}[\-\.\s]+?\d{4}\s*';
foreach $v (@phones) {
print "$v ";
if ($v =~ /^$reg$/) { print " matches\n"; } else { print "NO MATCH\n"; }
########## END OF SCRIPT #################

Note the actual regexp is stored in a variable, which you can use in PHP like so:

if (preg_match("/^$reg$/",$phone)) { echo 'ok'; }

An analysis, which you can use to dissect/remove/add elements:

\s* = zero or more leading spaces, remove this if you plan to remove them before matching, which is probably a good idea, remove leading/trailing spaces

not in this regexp: if you want an optional leading +, add this here:


\(* = zero or more (. Zero or more makes it OPTIONAL

\d{2,3} = between two or three leading numbers. (Project this was from had options of 2). If you just want 3, do \d{3}

\s* = zero or more spaces

\)* = zero or more )

[\-\.\s]+? = one or more of **any** of these. In this it's required,, if you want a separator optional, use [\-\.\s]*? instead.

\d{3,4} = between 3 or 4 digits, again, this is project specific, for the U.S. this is usually \d{3}

[\-\.\s]+? = same as above, if you want it optional use [\-\.\s]*?

\d{4} = ends with 4 digits only

\s* = possible trailing space, remove if you plan to strip these off first

10:11 pm on Dec 17, 2009 (gmt 0)

Junior Member

5+ Year Member

joined:Mar 26, 2008
votes: 0

I was thinking of using it in something like a contact form for later reply.

I filled one out recently and I was bugged by their formatting rule being too stiff. I imagine in a database app, you would want to be more fussy, but otherwise you would be more lax.

Might it even be worth while from a customer service standpoint to permit this on the client side, and then when it gets over to the server side, either accept as is, or modify to fit depending on your data base scheme.?


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members