📞 Highly customizable phone input component with auto formatting
Installations
npm install react-phone-input-2
Score
83.4
Supply Chain
93
Quality
75.2
Maintenance
100
Vulnerability
98.6
License
Releases
Unable to fetch releases
Developer
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
18.0.0
NPM Version
8.6.0
Statistics
957 Stars
375 Commits
543 Forks
8 Watching
1 Branches
71 Contributors
Updated on 27 Nov 2024
Bundle Size
54.45 kB
Minified
17.19 kB
Minified + Gzipped
Languages
JavaScript (67.15%)
Less (32.53%)
HTML (0.32%)
Total Downloads
Cumulative downloads
Total Downloads
48,906,946
Last day
-5.3%
71,715
Compared to previous day
Last week
-0.4%
368,521
Compared to previous week
Last month
9%
1,564,764
Compared to previous month
Last year
17.2%
16,737,346
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
React-Phone-Input-2
Highly customizable phone input component with auto formatting.
Installation
1npm install react-phone-input-2 --save
Usage
1import PhoneInput from 'react-phone-input-2' 2import 'react-phone-input-2/lib/style.css' 3 4<PhoneInput 5 country={'us'} 6 value={this.state.phone} 7 onChange={phone => this.setState({ phone })} 8/>
available styles - style • high-res • material • bootstrap • semantic-ui • plain
Demo 1 (UI) - Demo 2 (CSS)
Options
Name | Type | Description | Example |
---|---|---|---|
country | string | initial country | 'us' | 1 |
value | string | input state value | |
onlyCountries | array | country codes to be included | ['cu','cw','kz'] |
preferredCountries | array | country codes to be at the top | ['cu','cw','kz'] |
excludeCountries | array | array of country codes to be excluded | ['cu','cw','kz'] |
placeholder | string | custom placeholder | |
inputProps | object | props to pass into the input |
Booleans | Default | Description |
---|---|---|
autoFormat | true | on/off phone formatting |
disabled | false | disable input and dropdown |
disableDropdown | false | disable dropdown only |
disableCountryCode | false | |
enableAreaCodes | false | enable local codes for all countries |
enableTerritories | false | enable dependent territories with population of ~100,000 or lower |
enableLongNumbers | false | boolean/number |
countryCodeEditable | true | |
enableSearch | false | enable search in the dropdown |
disableSearchIcon | false | hide icon for the search field |
1<PhoneInput 2 inputProps={{ 3 name: 'phone', 4 required: true, 5 autoFocus: true 6 }} 7/>
Contents
- Style
- Events
- Regions
- Localization
- Local area codes
- Custom masks
- Custom area codes
- Other props
- Custom localization
- Guides
- Contributing
- Support
Style
containerClass | string | class for container | |
inputClass | string | class for input | |
buttonClass | string | class for dropdown button | |
dropdownClass | string | class for dropdown container | |
searchClass | string | class for search field | |
containerStyle | object | styles for container | |
inputStyle | object | styles for input | |
buttonStyle | object | styles for dropdown button | |
dropdownStyle | object | styles for dropdown container | |
searchStyle | object | styles for search field |
Events
onChange | onFocus | onBlur | onClick | onKeyDown |
onChange(value, country, e, formattedValue)
Country data object not returns from onKeyDown event
Data | Type | Description |
---|---|---|
value/event | string/object | event or the phone number |
country data | object | country object { name, dialCode, countryCode (iso2) } |
Regions
Name | Type | Description |
---|---|---|
regions | array/string | to show countries only from specified regions |
Regions |
---|
['america', 'europe', 'asia', 'oceania', 'africa'] |
Subregions |
['north-america', 'south-america', 'central-america', 'carribean', 'eu-union', 'ex-ussr', 'ex-yugos', 'baltic', 'middle-east', 'north-africa'] |
1<PhoneInput 2 country='de' 3 regions={'europe'} 4/> 5 6<PhoneInput 7 country='us' 8 regions={['north-america', 'carribean']} 9/>
Predefined localization
es
Spanish, de
Deutsch, ru
Russian, fr
French
jp
Japanese, cn
Chinese, pt
Portuguese, it
Italian
ir
Iranian, ar
Arabic, tr
Turkish, id
Indonesian
hu
Hungarian, pl
Polish, ko
Korean
1import es from 'react-phone-input-2/lang/es.json' 2 3<PhoneInput 4 localization={es} 5/>
Local area codes
1<PhoneInput 2 enableAreaCodes={true} 3 enableAreaCodes={['us', 'ca']} 4 enableAreaCodeStretch 5/>
Flexible mask
If enableAreaCodeStretch
is added, the part of the mask with the area code will not stretch to length of the respective section of phone mask.
Example: +61 (2), +61 (02)
Custom masks
1<PhoneInput 2 onlyCountries={['fr', 'at']} 3 masks={{fr: '(...) ..-..-..', at: '(....) ...-....'}} 4/>
Custom area codes
1<PhoneInput 2 onlyCountries={['gr', 'fr', 'us']} 3 areaCodes={{gr: ['2694', '2647'], fr: ['369', '463'], us: ['300']}} 4/>
Other props
defaultMask | ... ... ... ... .. |
alwaysDefaultMask | false |
prefix | + |
searchPlaceholder | 'search' |
searchNotFound | 'No entries to show' |
copyNumbersOnly | true |
renderStringAsFlag | string |
autocompleteSearch | false |
jumpCursorToEnd | false |
priority | {{ca: 0, us: 1, kz: 0, ru: 1}} |
enableClickOutside | true |
showDropdown | false |
defaultErrorMessage | string |
specialLabel | string |
disableInitialCountryGuess | false |
disableCountryGuess | false |
Custom localization
1<PhoneInput 2 onlyCountries={['de', 'es']} 3 localization={{de: 'Deutschland', es: 'España'}} 4/> 5 6<PhoneInput 7 onlyCountries={['de', 'es']} 8 localization={{'Germany': 'Deutschland', 'Spain': 'España'}} 9/>
Preserve countries order
1<PhoneInput 2 onlyCountries={['fr', 'at']} 3 preserveOrder={['onlyCountries', 'preferredCountries']} 4/>
Guides
Phone without dialCode
1handleOnChange(value, data, event, formattedValue) {
2 this.setState({ rawPhone: value.slice(data.dialCode.length) })
3}
Check validity of the phone number
isValid(value, country, countries, hiddenAreaCodes)
1<PhoneInput 2 isValid={(value, country) => { 3 if (value.match(/12345/)) { 4 return 'Invalid value: '+value+', '+country.name; 5 } else if (value.match(/1234/)) { 6 return false; 7 } else { 8 return true; 9 } 10 }} 11/>
1import startsWith from 'lodash.startswith'; 2 3<PhoneInput 4 isValid={(inputNumber, country, countries) => { 5 return countries.some((country) => { 6 return startsWith(inputNumber, country.dialCode) || startsWith(country.dialCode, inputNumber); 7 }); 8 }} 9/>
Clear country
To clear country
, pass null
as value
.
Dynamic placeholder
Show
1const phoneCountryFormat = useMemo(() => phoneCountry?.format || undefined, [phoneCountry]); 2const placeholder = useMemo(() => { 3 if (phoneCountryFormat) { 4 const words = phoneCountryFormat.split(' '); 5 words.shift(); // I'm using only local numbers so here I remove the country code from the format 6 let text = words.join(' '); 7 // first digit is special on french numbers, these 3 lines could be removed 8 if (country === 'fr') { 9 text = text.replace('.', '6'); 10 } 11 while (text.indexOf('.') > -1) { 12 text = text.replace('.', `${Math.min(9, Math.max(1, Math.floor(Math.random() * 10)))}`); 13 } 14 return text; 15 } 16 return ''; 17}, [phoneCountryFormat, country]);
CDN
1<script src="https://unpkg.com/react-phone-input-2@2.x/dist/lib.js"></script>
Contributing
- Code style changes not allowed
- Do not create issues about incorrect or missing country masks (of already present countries) or absent area codes (they will be closed). Only create issues if the subject is an actual mechanism that is not present in the component. Otherwise create a PR with a link that proves the correctness of your newly suggested mask or list of area codes
- Do not send new languages
License
Based on react-phone-input
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 23/30 approved changesets -- score normalized to 7
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 23 are checked with a SAST tool
Score
4
/10
Last Scanned on 2024-11-25
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 MoreOther packages similar to react-phone-input-2
material-ui-phone-number
A material-ui react component to format phone numbers. Based on react-phone-input-2
react-phone-input-material-ui
A material-ui react component to format phone numbers. Based on react-phone-input-2
mui-phone-number
A material-ui v5+ react component to format phone numbers. Based on react-phone-input-2
react-phone-input-international
A react component to format phone numbers. Fork from https://github.com/bl00mber/react-phone-input-2