Page 1 of 1

Phone number matching

Posted: Thu Oct 25, 2007 12:58 am
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

Posted: Thu Oct 25, 2007 1:22 am
by Kieran Huggins

Posted: Thu Oct 25, 2007 3:55 am
by Benjamin
WOW, that is sooo cheating.

Posted: Thu Oct 25, 2007 4:15 am
by Kieran Huggins
Yes, yes it is 8)

Re: Phone number matching

Posted: Thu Oct 25, 2007 5:07 am
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']);

Posted: Thu Oct 25, 2007 5:30 am
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?

Posted: Thu Oct 25, 2007 11:43 am
by GeertDD
Phone numbers can contain letters? :?

(Well, then my cleaning regex won't be sufficient anymore, of course.)

Posted: Thu Oct 25, 2007 2:30 pm
by mrkite
GeertDD wrote:Phone numbers can contain letters? :?
How else would you enter a phone number with an extension?

Posted: Thu Oct 25, 2007 3:25 pm
by feyd
I would probably opt for a separate field for extensions.