Forum Moderators: mack
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.
/^(\d{3}-¦\(\d{3}\))\d{3}-\d{4}$/
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.
/^(\d{3}-¦\(\d{3}\))\d{3}-\d{4}$/
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.
#!/usr/bin/perl
@phones = (
'02 9988 1288',
'(02) 9988 1288',
'(02)-9988-1288',
'(02).9988.3288',
'02.9988 1288',
'123 456 1288',
'(123 ) 456 1288',
'(123)-456-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:
\+*\s*
\(* = 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
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.?