根据国家/地区代码设置电话号码的格式

Formatting phone numbers based on country code

本文关键字:设置 电话号码 格式 代码 地区 国家      更新时间:2023-09-26

我有一个包含电话号码的数据库,我想根据电话号码的国家/地区代码对其进行格式化。

这些数字当前以以下格式存储:

+[country_code][number] (like +4512345678)

我需要的是某种用于javascript的库,它基于国家代码可以根据官方的国家格式(http://en.wikipedia.org/wiki/National_conventions_for_writing_telephone_numbers)格式化数字。下面是一些如何格式化某些数字的示例

+4512345678 => +45 12 34 56 78
+12892708970 => (289) 270-8970

我在这里找到了这个Java库 https://code.google.com/p/libphonenumber/,它基本上解决了这个问题。但是,我想要一个简单的 Javascript 解决方案,他们实际上在他们的页面上演示了该解决方案,但我找不到他们使用的文件文件的任何官方下载,这让我想知道是否存在更好的解决方案?

使用 libphonenumber-js https://github.com/googlei18n/libphonenumber

它具有所有必需的功能。

信用

http://libphonenumber.googlecode.com/svn/trunk/javascript/i18n/phonenumbers/- 也许这就是您要在同一个库中寻找的?

尝试jQuery Validate lib,它包含我们,你可以添加自己的验证。

http://jqueryvalidation.org/phoneUS-method

另一个

libphonenumber库(libphonenumber-js)重写了Google Android的libphonenumber库。

在此处查看实时示例。

https://catamphetamine.github.io/libphonenumber-js/