homepage Welcome to WebmasterWorld Guest from 54.211.34.105
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / WebmasterWorld / New To Web Development
Forum Library, Charter, Moderators: brotherhood of lan & mack

New To Web Development Forum

    
Sensible Regex
This works, but is it a good idea?
Baruch Menachem

5+ Year Member



 
Msg#: 4044532 posted 3:48 am on Dec 17, 2009 (gmt 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.

/^(\d{3}-¦\(\d{3}\))\d{3}-\d{4}$/

 

piatkow

WebmasterWorld Senior Member piatkow us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 4044532 posted 9:49 am on Dec 17, 2009 (gmt 0)

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.

rocknbil

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



 
Msg#: 4044532 posted 8:37 pm on Dec 17, 2009 (gmt 0)

/^(\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

Baruch Menachem

5+ Year Member



 
Msg#: 4044532 posted 10:11 pm on Dec 17, 2009 (gmt 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.?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / WebmasterWorld / New To Web Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved