Verification of phone numbers is a frequent requirement in real projects. Looking for a circle of online open source verification library, did not find a satisfactory work. They either verify that the conditions are ancient and not adapted to the development of The Times; Or validation is too dead to be friendly to new segments that may appear in the future. So I decided to build my own wheel.

Telecommunications Network Numbering Scheme (2017 Version)

After several searches, I found the official organization that manages China’s telephone number resources — The Ministry of Industry and Information Technology of China. In the integrated Resource Management System of its sub-website, I found the Telecom network numbering plan (2017 edition). The plan provides the following key information:

Number structure

Fixed-line telephone number

China’s fixed network telephone numbers adopt the structure of long distance area code, namely:

Country code (86) + Toll area code + local user number

Fixed network telephone numbers are numbered in unequal digits, and the maximum length of valid domestic numbers is 11 digits. Local user number length 7/8 digits coexist.

Mobile phone number

China’s mobile network telephone number adopts the network number structure, namely:

Country Code (86) + Network Number + HLR Id (H0H1H2H3) + User Number (ABCD)

The network number of the public mobile network is 3 digits, with an equal number, and the length of the valid national number is 11 digits.

Distribution of them roughly

The first to1

In principle, a number that starts with 1 should be used nationwide and can be divided into service numbers and user numbers. Its planning is as follows:

The first to2 ~ 8

Numbers with the first digit from 2 to 8 are used locally. They are mainly used as subscriber numbers of the fixed local telephone network. Some numbers with the first digit from 2 to 8 are used as access codes for smart services nationwide and in provinces.

The first to9

Segment 92 and 98 numbers are planned for the public mobile communication network phone numbers. They are numbered by an equal number and have a valid national number length of 11 digits. Other numbers starting with 9 are national or stand-by.

China fixed telephone network long distance area code

The area code contains 2 to 3 characters and must not start with 0.

Verify Chinese mobile and landline numbers using JavaScript

Based on the above information, I wrote the following re authentication phone number:

Note: In the strict mode of mobile phone number verification, the number segment used is the public mobile communication network number described in the above plan, which does not guarantee that it has been opened and operated by the telecom operator.

Type the number Loose pattern Strict mode
Mobile phone Loose segment verification:/ ^ 1 [3-9] [0-9] {9} $/ More stringent number segment verification:/ ^ 1 (? : 3 [0-9] [5-9] | | 4 5 6 [12456] [0-9] | | 7-8 0 and 8 [0-9] | | 9 [0-9] [0-9] {8} $/
landline Area code optional:/ ^ (? : 0 [1-9] [0-9] {1, 2} -)? [2-8] [0-9] {6, 7} $/ Area code Required:/ ^ 0 [1-9] [0-9] {1, 2} - [2-8] [0-9] {6, 7} $/

And released the IS-Chinese-phone-number package:

The installation

# yarn
yarn add is-chinese-phone-number

# or, npm
npm i is-chinese-phone-number
Copy the code

CDN: jsDelivr | UNPKG (can be used by global variables isChinesePhoneNumber)

use

Verify the Chinese phone number

Grammar:

isChinesePhoneNumber(phoneNumber: string | number, strict: boolean = false): boolean
Copy the code

Example:

isChinesePhoneNumber('10086') // ==> false
isChinesePhoneNumber('18087030020') // ==> true
isChinesePhoneNumber('010-88888888') // ==> true
Copy the code

Verify Chinese mobile phone number

Grammar:

isChinesePhoneNumber.mobile(phoneNumber: string | number, strict: boolean = false): boolean
Copy the code

Example:

isChinesePhoneNumber.mobile('10086') // ==> false
isChinesePhoneNumber.mobile('18087030020') // ==> true
isChinesePhoneNumber.mobile('010-88888888') // ==> false
Copy the code

Verify China landline number

Grammar:

isChinesePhoneNumber.landline(phoneNumber: string | number, strict: boolean = false): boolean
Copy the code

Example:

isChinesePhoneNumber.landline('10086') // ==> false
isChinesePhoneNumber.landline('18087030020') // ==> false
isChinesePhoneNumber.landline('010-88888888') // ==> true
Copy the code