最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - whats the universally recognised telephone# format - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Aug 29, 2014 at 20:25 Kara 6,22616 gold badges53 silver badges58 bronze badges asked Mar 21, 2011 at 13:00 Sourabh ShankarSourabh Shankar 3753 silver badges8 bronze badges 5
  • I suppose the only thing you could do is make sure it only contains + - 0-9 and space – JohnP Commented Mar 21, 2011 at 13:04
  • 1 @JohnP: Also, "x", for extension. E.g. 12345x789 is phone nr. 12345, extension 678. – Sander Marechal Commented Mar 21, 2011 at 13:18
  • stackoverflow./questions/3805349/… – Incognito Commented Mar 21, 2011 at 13:22
  • What about vanity numbers aka phonewords, e.g. 1-555-FOOBARBAZ ? – Ocaso Protal Commented Mar 21, 2011 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 Commented Apr 17, 2023 at 15:42
Add a ment  | 

6 Answers 6

Reset to default 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 pany 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.

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.

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.

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.

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

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

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).

发布评论

评论列表(0)

  1. 暂无评论