Phone number matching

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

Phone number matching

Post by shivam0101 »

I want to validated phone number. It is in the pattern, digits 0-9 and - (hyphen can be present)

I tried to

Code: Select all

if(!eregi('^[0-9-] +', $phone))
                                     array_push($mistake, 'Invalid Phone number');
even if i give characters its taking
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

WOW, that is sooo cheating.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Yes, yes it is 8)
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Re: Phone number matching

Post by GeertDD »

shivam0101 wrote:I want to validated phone number. It is in the pattern, digits 0-9 and - (hyphen can be present)
Why not let the user choose the format he wishes to use for phone input, be it dashes, spaces, slashes, dots, whatever. Let your script clean it up and then just check the numbers. I mean, you don't want to go show errors like "Your phone number must not contain spaces, please remove them", do you?

Code: Select all

// Clean phone number
$phone = preg_replace('/\D+/', '', $_POST['phone']);
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

In my experience, phone numbers and addresses should be collected in the user's preferred style.

Addresses can often be goelocated using the Google Maps API, but should remain in their original form for storage and display. I say leave the heavy lifting to Google if you can.

Phone numbers are not all that dissimilar, even though as programmers we instinctually want to mangle them into a collection of digits. I've found that enough people want to input phone numbers in letter form (ex: 1-800-5-flower) that this can become problematic. Simply exchanging letters for their numeric equivalent seems like a simple task, but this can lead to undesirable results at best, and will mangle the data at worst. Imagine standard extension notation: 555-1212 x123 / 555-1212 ext 123 / 1 800 5-flower x123.

While we can implement algorithms to translate most user input properly, the margin for error is still too high for my comfort level. Also, users would likely prefer the letter form on the way out rather than the numeric equivalent. Since there's no Google Phone API (at least not yet) it seems like the best course of action is to create our own phone number cleaner (which I've done to some success, though only for N.American and UK numbers).

At any rate, if a number doesn't make for an easy translation I prefer to leave it entirely un-mangled and if necessary leave it to human interpretation.

Anyone have a better plan?
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

Phone numbers can contain letters? :?

(Well, then my cleaning regex won't be sufficient anymore, of course.)
mrkite
Forum Contributor
Posts: 104
Joined: Tue Sep 11, 2007 4:19 am

Post by mrkite »

GeertDD wrote:Phone numbers can contain letters? :?
How else would you enter a phone number with an extension?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I would probably opt for a separate field for extensions.
Post Reply