Gathering detailed insights and metrics for phone
Gathering detailed insights and metrics for phone
Gathering detailed insights and metrics for phone
Gathering detailed insights and metrics for phone
react-phone-input-2
A react component to format phone numbers
react-international-phone
☎️ International phone input component for React
react-native-phone-number-input
Phone Number Input Component
material-ui-phone-number
A material-ui react component to format phone numbers. Based on react-phone-input-2
With a given country and phone number, validate and reformat the mobile phone number to the E.164 standard. The purpose of this is to allow us to send SMS to mobile phones only.
npm install phone
99.5
Supply Chain
99.6
Quality
94.1
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
852 Stars
588 Commits
269 Forks
43 Watching
1 Branches
310 Contributors
Updated on 27 Nov 2024
Minified
Minified + Gzipped
TypeScript (58.45%)
JavaScript (39.94%)
HTML (1.61%)
Cumulative downloads
Total Downloads
Last day
-13.7%
34,599
Compared to previous day
Last week
-3.9%
192,861
Compared to previous week
Last month
7.9%
848,808
Compared to previous month
Last year
46.7%
8,262,498
Compared to previous year
25
phone
is used to normalize mobile 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
npm install phone
// or
yarn add phone
1const {phone} = require('phone'); 2 3// or 4 5import {phone} from 'phone';
1phone('+852 6569-8900'); 2// { isValid: true, phoneNumber: '+85265698900', countryIso2: 'HK', countryIso3: 'HKG', countryCode: '+852' }
1phone('+1(817) 569-8900', {country: ''}); 2// { isValid: true, phoneNumber: '+18175698900', countryIso2: 'US', countryIso3: 'USA', countryCode: '+1'} 3 4phone('(817) 569-8900', {country: 'USA'}); 5// { isValid: true, phoneNumber: '+18175698900', countryIso2: 'US', countryIso3: 'USA', countryCode: '+1'} 6 7phone('(817) 569-8900', {country: 'HKG'}); 8// { isValid: false } 9// not a valid HKG mobile phone number 10 11phone('+1(817) 569-8900', {country: 'HKG'}); 12// { isValid: false } 13// not a valid HKG mobile phone number 14 15phone('6123-6123', {country: 'HKG'}); 16// { isValid: true, phoneNumber: '+85261236123', countryIso2: 'HK', countryIso3: 'HKG', countryCode: '+852' }
If both country code and country phone prefix are not provided, the phone number will be treated as USA or Canada by default.
1phone('(817) 569-8900'); 2// { isValid: true, phoneNumber: '+18175698900', countryIso2: 'US', countryIso3: 'USA', countryCode: '+1' } 3 4phone('(817) 569-8900', {country: ''}); 5// { isValid: true, phoneNumber: '+18175698900', countryIso2: 'US', countryIso3: 'USA', countryCode: '+1' } 6 7phone('780-569-8900', {country: null}); 8// { isValid: true, phoneNumber: '+17805698900', countryIso2: 'CA', countryIso3: 'CAN', countryCode: '+1' } 9// 780 is a Canada phone prefix 10 11phone('6123-6123', {country: null}); 12// { isValid: false } 13// as default country is USA / CAN and the phone number does not fit such countries' rules
+
signEven you input a valid phone number with a valid prefix, if there is no plus sign, it will not work as expected:
1phone('85291234567'); 2// or 3phone('85291234567', {country: null}); 4 5// { isValid: false }
852
is a valid Hong Kong phone prefix, and 91234567
is a valid Hong Kong mobile phone number.
However, there is no plus sign provided, the module will assume the phone number is a USA or Canada phone number,
hence no result will be found.
If you know you have provided country phone prefix, make sure you also provide a plus sign:
1phone('+85291234567'); 2// or 3phone('+85291234567', {country: null}); 4 5// { isValid: true, phoneNumber: '+85291234567', countryIso2: 'HK', countryIso3: 'HKG', countryCode: '+852' }
or, if you know the country, and only want to reformat the phone number to E.164 format:
1phone('91234567', {country: 'HKG'}) 2// { isValid: true, phoneNumber: '+85291234567', countryIso2: 'HK', countryIso3: 'HKG', countryCode: '+852' }
If you want to skip phone number initial digit checking, set validateMobilePrefix
to false:
1phone('+(852) 2356-4902'); 2// { isValid: false } 3// '2' is a Hong Kong landline phone number prefix, not a valid mobile phone number prefix 4 5phone('+(852) 2356-4902', {validateMobilePrefix: true}); 6// { isValid: false } 7// same as above, default value of validateMobilePrefix = true 8 9phone('+(852) 2356-4902', {validateMobilePrefix: false}); 10// { isValid: true, phoneNumber: '+85223564902', countryIso2: 'HK', countryIso3: 'HKG', countryCode: '+852' } 11// skipping mobile prefix checking
With validateMobilePrefix
set to false
, the initial digit checking logic will be disabled completely, even you enter a phone number start with a non-exist digit:
1phone('+(852) 0356-4902', {validateMobilePrefix: false}); 2// { isValid: true, phoneNumber: '+85203564902', countryIso2: 'HK', countryIso3: 'HKG', countryCode: '+852' } 3// even the phone number start with `0` is not a valid landline phone number
Note that the module does not have the capability to determine if the prefix is a valid landline
prefix number.
For some phone numbers, such as this sample UK phone number:
+44 07911 123456
There is a trunk code 0
after the country code +44
so that it is unable to match any correct country.
Hence the module will try to remove 1 digit after the country code,
and try to detect:
+44 7911 123456
and it would become a valid UK phone number now.
1phone('+4407911 123456') 2// { isValid: true, phoneNumber: '+447911123456', countryIso2: 'GB', countryIso3: 'GBR', countryCode: '+44' }
If you want to disable this behavior,
please set strictDetection
to true
:
1phone('+4407911 123456', {strictDetection: true}) 2// { isValid: false }
1const {phone} = require('phone'); 2 3// or 4 5import {phone} from 'phone'; 6 7phone(phoneNumber: string, { country, validateMobilePrefix, strictDetection }?: { 8 country?: string; 9 validateMobilePrefix?: boolean; 10 strictDetection?: boolean; 11})
Parameter | Type | Required | Default | Description |
---|---|---|---|---|
phoneNumber | String | Yes | - | The phone number text you want to process |
country | String | No | null | Provided country code in iso-3166 alpha 2 or 3 format |
validateMobilePrefix | Boolean | No | true | Set to false if you want to skip phone number initial digit checking |
strictDetection | Boolean | No | false | Set to true if you want to disable trunk code detection logic. |
1type PhoneResult = PhoneInvalidResult | PhoneValidResult; 2 3interface PhoneValidResult { 4 isValid: true; 5 phoneNumber: string; 6 countryIso2: string; 7 countryIso3: string; 8 countryCode: string; 9} 10 11interface PhoneInvalidResult { 12 isValid: false; 13 phoneNumber: null; 14 countryIso2: null; 15 countryIso3: null; 16 countryCode: null; 17}
Parameter | Type | Description |
---|---|---|
isValid | Boolean | To indicate if the result valid |
phoneNumber | String or null | Normalized phone number in E.164 format |
countryIso2 | String or null | Detected phone number country code in iso-3166 alpha 2 format |
countryIso3 | String or null | Detected phone number country code in iso-3166 alpha 3 format |
countryCode | String or null | Detected phone number country calling code with + sign |
yarn test
yarn start:example
or
yarn dev
And then visit http://localhost:8080
yarn build
Does phone
do any logical validation?
Yes. If you provide country
, and the phone number does not start with +
sign,
the module will validate phone_number_lengths
and mobile_begin_with
Why is phone
returning an invalid result for a valid phone number?
By default, the function will validate a mobile phone number only, to validate a landline phone number, please set validateMobilePrefix
to false
.
If you find the result is still incorrect, please submit a ticket to improve our validation rules.
Why is phone
returning an object with isValid = false
instead of returning a null directly?
It reserves the flexibility to extend the response interface for invalid results in the future.
The interface of v3 has been changed for better usability, maintainability, and flexibility, this shows all the changes from v2:
Version | Interface |
---|---|
v2 | phone(phoneNumber, country, allowLandline) |
v3 | phone(phoneNumber,{country: String, validateMobilePrefix: Boolean, strictDetection: Boolean}) |
Version | Result | Interface |
---|---|---|
v2 | - | [phoneNumber, country] |
v3 | Valid | {isValid: true, phoneNumber: string, countryIso2: string, countryIso3: string, countryCode: string} |
v3 | Invalid | {isValid: false, phoneNumber: null, countryIso2: null, countryIso3: null, countryCode: null} |
allowLandline
in v2 is essentially equal to validateMobilePrefix
in v3, however, the value is the opposite.
Because allowLandline = true
in v2 means "Skip the mobile phone number prefix validation", and there is NO capability to verify if the input phone number is a valid landline phone number.
To avoid misleading information, the parameter name has been changed to validateMobilePrefix
, and the input value is the opposite, while validateMobilePrefix = false
means "Skip the mobile phone number prefix validation".
We strive to ensure this package functions well across diverse scenarios. However, please note that the phone number formats may not be updated proactively or regularly, as this is a manual task and we lack a dedicated incentive for continuous updates. If you find any incorrect rules for a country or other specific case, please create a pull request to inform us.
When submitting pull requests to add or modify phone number formats, it is essential to include reference information such as PDFs, websites, etc. PRs submitted without references will not be accepted. Thank you for your understanding and cooperation.
The library supports mobile phone number format only. We are unable to provide landline phone number support as we do not have landline phone number format data, hence we do not accept PRs for landline phone numbers.
This project is licensed under the MIT license.
No vulnerabilities found.
Reason
18 commit(s) out of 30 and 8 issue activity out of 30 found in the last 90 days -- score normalized to 10
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
no binaries found in the repo
Reason
no vulnerabilities detected
Reason
update tool detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 9
Details
Reason
GitHub code reviews found for 13 commits out of the last 30 -- score normalized to 4
Details
Reason
no badge detected
Reason
non read-only tokens detected in GitHub workflows
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
project is not fuzzed
Reason
security policy file not detected
Score
Last Scanned on 2022-08-15
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More