1

What's the most widely accepted telephone number format worldwide.

Of I validate telephone# according to US format then non-US users will be having a hard time entering theirs and if I do vice-versa, then US users will be having a hard time.

I am looking for a universally recognized telephone number format which I am going validate through both JS and PHP.

Kara
  • 6,115
  • 16
  • 50
  • 57
Sourabh Shankar
  • 375
  • 3
  • 8
  • I suppose the only thing you could do is make sure it only contains `+` `-` `0-9` and `space` – JohnP Mar 21 '11 at 13:04
  • 1
    @JohnP: Also, "x", for extension. E.g. 12345x789 is phone nr. 12345, extension 678. – Sander Marechal Mar 21 '11 at 13:18
  • http://stackoverflow.com/questions/3805349/normalizing-validation-for-international-data-sets-in-a-database – Incognito Mar 21 '11 at 13:22
  • What about vanity numbers aka phonewords, e.g. 1-555-FOOBARBAZ ? – Ocaso Protal Mar 21 '11 at 13:54
  • Could not find anywhere, so here is a quick `function vanityToPhoneNumber(string $vanity):string { return preg_replace(['/[ABC]/', '/[DEF]/', '/[GHI]/', '/[JKL]/', '/[MNO]/', '/[PQRS]/', '/[TUV]/', '/[WXYZ]/'], [ 2, 3, 4, 5, 6, 7, 8, 9 ], $vanity); }` – Phil Tune Apr 17 '23 at 15:42

6 Answers6

3

Please, please, don't validate anything more than valid characters (as has been suggested, digits, dashes, plusses and possibly spaces/brackets). At least do not make your validation mandatory: if the phone number doesn't match, just warn the user. You're not going to be able to match each and every potential format: these vary not just by country, but to some extent by phone company and region (at least with respect to the number of digits).

The purpose of validation should be to help end users enter correct information. It requires that you know more than they do, and are prepared to alter your validation when what is correct shifts.

Pontus Gagge
  • 17,166
  • 1
  • 38
  • 51
  • I would also add an 'x' as a valid character, to include an extension. Ex: +1 (777) 444.5555 x12345. Seconded that it would be very difficult to completely validate a phone number. – Zimzat Mar 21 '11 at 13:24
  • Validation also has to be against an intended usage. Is the phone number to be used by a person dialling it manually? Or as a substitute primary key? Or for (semi)automatic dialling, e.g. in a phone book? 'x' is bad in the last case, but not the former two. Spaces are good in the first and last, but not the middle case. And so on... – Pontus Gagge Mar 21 '11 at 13:39
3

If you take a look at rfc3966 it explains what is considered a valid phone number (specifically section 5.1).

However with that said you shouldn't worry about how a user enters a phone number, strip out all formating when they submit, validate the number and then store it without formating.

Handle reformatting the number when you pull it from the database to display it.

Community
  • 1
  • 1
tplaner
  • 8,363
  • 3
  • 31
  • 47
  • Yep, +NNNN... is the universal format and should be accepted by any phone number entering system. Sadly too many bad web developers forget that phone numbers are supposed to start with "+". – sorin Feb 18 '16 at 15:59
2

The only thing you can validate is that it contains numbers. The amount of numbers varies from country to country.

Check out the Local conventions for writing telephone numbers Wikipedia article.

Nico
  • 2,645
  • 19
  • 25
1

is there any wiki where I can find telephone format for individual countries

You can try: http://en.wikipedia.org/wiki/Category:Telephone_numbers_by_country

stema
  • 90,351
  • 20
  • 107
  • 135
g1smd
  • 43
  • 2
1

You need to differentiate your users based on their location. So user must select his location (US for example) first then you need to validate his phone number against US format.

fabrik
  • 14,094
  • 8
  • 55
  • 71
0

None is sufficiently widespread that it can be seen as if-it-does-not-fit-it-is-not-a-telephone-number. The format '+' CountryCode ' ' (AreaCode ' ')? SubscriberNumber is standardized by the ITU. CountryCode, AreaCode and SubscriberNumber are sequences of digits.

Then again, in Germany the AreaCode often begins with (0) (indicating that you have to dial it if you do not dial the CountryCode). People also tend to group the digits of the SubscriberNumber or put all sorts of punctuation in it (for whatever purpose).

All in all, validating a telephone number is a hassle. Either do it very strict and annoy your users, or do the bare minimum (contains at least one digit).

Oswald
  • 31,254
  • 3
  • 43
  • 68