@foerster-technologies/phonenumbers

中文文档
1.0.2发布于 2020/08/05MITCommonJS零依赖近 30 天下载 2328

With a given country and phone number, validate and format the phone number to E.164 standard

原始使用文档
文档目录12 节
  1. What is phonenumbers?
  2. Install
  3. Demo
  4. Usage
  5. API
  6. phone(phone: String, [country: string, allowLandline: Boole…
  7. Test
  8. Build
  9. Old browsers & browser support
  10. FAQ
  11. Help
  12. License

Phone · PRs Welcome

What is phonenumbers?

This is a fork of the phone package.

phonenumber is used to normalize mobile and land line phone numbers into E.164 format.

A common problem is that users normally input phone numbers in this way:

`(817) 569-8900` or
`817569-8900` or
`1(817) 569-8900` or
`+1(817) 569-8900` or ...

We always want:

+18175698900

Install

npm install phone

or

yarn add phone

Demo

From phone Try it on CodeSandbox

Usage

const phone = require('phonenumbers');

phone('+852 6569-8900'); // return ['+85265698900', 'HKG']
phone('+1(817) 569-8900', ''); // return ['+18175698900', 'USA']
phone('(817) 569-8900', 'USA'); // return ['+18175698900', 'USA']
phone('(817) 569-8900', 'HKG'); // return []
phone('+1(817) 569-8900', 'HKG'); // return [], as it is not a valid HKG mobile phone number
phone('6123-6123', 'HKG'); // return ['+85261236123', 'HKG']

If both country code and country phone prefix are not provided, will treat as USA or Canada by default

phone('(817) 569-8900'); // return ['+18175698900, 'USA']
phone('(817) 569-8900', ''); // return ['+18175698900, 'USA']
phone('(817) 569-8900', ''); // return ['+18175698900', 'USA']
phone('780-569-8900', ''); // return ['+17805698900, 'CAN'], 780 is a Canada phone prefix
phone('6123-6123', ''); // return [], as default country is USA / CAN and it does not match any result

If you want to skip phone number initial digit checking, set allowLandline to true:

phone('+(852) 2356-4902', '', true);

And the initial digit checking will be disabled completely, even you enter a phone number start with a non-exist digit:

phone('+(852) 0356-4902', '', true); // return [ '+85203564902', 'HKG' ], even the phone number start with `0` is not a valid landline phone number

API

const phone = require('phonenumbers');

phone(phone: String, [country: string, allowLandline: Boolean]): Array

Input

ParameterTypeRequiredDescription
phoneStringYesThe phone number text you want to process
countryStringNoProvided country code in iso-3166 alpha 2 or 3 format
allowLandLineBooleanNoSet to true if you want to skip phone number initial digit checking

Returns

The return would always be an Array

Array indexTypeDescription
0StringNormalized phone number in E.164 format
1StringDetected phone number country code in iso-3166 alpha 3 format

If the phone number cannot be reformatted due to any reason (e.g. unable to match any country), the result would be an empty array.

The function response is in Array format for some historical reason and expected to be updated to a proper object response in the next major version bump. (Will not change on version 2.x)

Test

yarn test

Build

yarn build

Old browsers & browser support

We currently transpile script to work on target environments for which the browser's global usage is >1%, and Node.js 6.10+.

You can check browser usage statistics on the browserlist.

You may need polyfills for some older browsers; for more details, please read the example/README file.

FAQ

  1. Does phonenumber do any logical validation?

    Yes. If you provide the 2nd parameter (country), and the phone number does not start with + sign.

    phonenumber will validate mobile_number_lengths and mobile_begin_with

  2. Why is phonenumber returning null for a valid phone number?

    By default, the function will validate a mobile phone number only, to validate a landline phone number, please set 3rd parameter allowLandline to true.

    If you find the result is still incorrect, please submit a ticket to improve our validation rules.

  3. How does allowLandline work?

    Mobile phone number detection is achieved by comparing the beginning digits of a phone number to a list of possible mobile prefixes for a country. In some countries, some or all of these prefixes are the same for both mobile phones and landlines; when allowLandline is set to false you should not assume that all possible landline phone numbers will be reliably filtered out.

Help

We've tried to make sure that this package works for as many cases as possible, if you notice that we have an incorrect rule for a country or other case, please open an issue to let us know.

For creating new pull requests regarding add or modify phone number formats, please include the reference information such as PDFs, websites, etc. Thank you very much.

License

This project is licensed under the MIT license.

版本与发布记录

@foerster-technologies/phonenumbers 共发布 3 个版本,首次发布于 2020/08/05;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.2latest2020/08/056 年前
1.0.12020/08/056 年前
1.0.02020/08/056 年前

关于 @foerster-technologies/phonenumbers 的常见问题

@foerster-technologies/phonenumbers 是什么?

With a given country and phone number, validate and format the phone number to E.164 standard

如何安装 @foerster-technologies/phonenumbers?

在项目目录运行 npm install @foerster-technologies/phonenumbers,或使用 pnpm add / yarn add / bun add。

@foerster-technologies/phonenumbers 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/foerster-technologies__phonenumbers 是否存在。

@foerster-technologies/phonenumbers 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@foerster-technologies/phonenumbers 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=6.10.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@foerster-technologies/phonenumbers 可以商用吗?

@foerster-technologies/phonenumbers 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@foerster-technologies/phonenumbers 的下载量有多少?

根据 npm 官方下载统计,@foerster-technologies/phonenumbers 在 2026/10/15 – 2026/11/13 这 30 天内被下载 2,328 次,日均约 78 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@foerster-technologies/phonenumbers 还在维护吗?

最新版本 1.0.2 发布于 2020/08/05(6 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/08/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。