2.15.1 • Published 2 years ago

react-phone-input-2 v2.15.1

Weekly downloads
96,733
License
MIT
Repository
github
Last release
2 years ago

React-Phone-Input-2

DONATIONS / FUNDING NEEDED

bitcoin bc1qa7q6gf9e06ssqrc46j5lekq886rlyyk6sdhjj0 ethereum 0x2b9429a7362a9b14f3d84a408d33caab29bdc225


Highly customizable phone input component with auto formatting.

npm version npm downloads PRs Welcome travis build

animation

Installation

npm install react-phone-input-2 --save

Usage

import PhoneInput from 'react-phone-input-2'
import 'react-phone-input-2/lib/style.css'

<PhoneInput
  country={'us'}
  value={this.state.phone}
  onChange={phone => this.setState({ phone })}
/>

available styles - style • high-res • material • bootstrap • semantic-ui • plain

Demo 1 (UI) - Demo 2 (CSS)

screenshot

Options

<PhoneInput
  inputProps={{
    name: 'phone',
    required: true,
    autoFocus: true
  }}
/>

Contents

Style

Events

onChange(value, country, e, formattedValue)

Country data object not returns from onKeyDown event

Regions

<PhoneInput
  country='de'
  regions={'europe'}
/>

<PhoneInput
  country='us'
  regions={['north-america', 'carribean']}
/>

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

import es from 'react-phone-input-2/lang/es.json'

<PhoneInput
  localization={es}
/>

Local area codes

<PhoneInput
  enableAreaCodes={true}
  enableAreaCodes={['us', 'ca']}
  enableAreaCodeStretch
/>

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

<PhoneInput
  onlyCountries={['fr', 'at']}
  masks={{fr: '(...) ..-..-..', at: '(....) ...-....'}}
/>

Custom area codes

<PhoneInput
  onlyCountries={['gr', 'fr', 'us']}
  areaCodes={{gr: ['2694', '2647'], fr: ['369', '463'], us: ['300']}}
/>

Other props

Custom localization

<PhoneInput
  onlyCountries={['de', 'es']}
  localization={{de: 'Deutschland', es: 'España'}}
/>

<PhoneInput
  onlyCountries={['de', 'es']}
  localization={{'Germany': 'Deutschland', 'Spain': 'España'}}
/>

Preserve countries order

<PhoneInput
  onlyCountries={['fr', 'at']}
  preserveOrder={['onlyCountries', 'preferredCountries']}
/>

Guides

Phone without dialCode

handleOnChange(value, data, event, formattedValue) {
  this.setState({ rawPhone: value.slice(data.dialCode.length) })
}

Check validity of the phone number

isValid(value, country, countries, hiddenAreaCodes)

<PhoneInput
  isValid={(value, country) => {
    if (value.match(/12345/)) {
      return 'Invalid value: '+value+', '+country.name;
    } else if (value.match(/1234/)) {
      return false;
    } else {
      return true;
    }
  }}
/>
import startsWith from 'lodash.startswith';

<PhoneInput
  isValid={(inputNumber, country, countries) => {
    return countries.some((country) => {
      return startsWith(inputNumber, country.dialCode) || startsWith(country.dialCode, inputNumber);
    });
  }}
/>

Clear country

To clear country, pass null as value.

Dynamic placeholder

const phoneCountryFormat = useMemo(() => phoneCountry?.format || undefined, [phoneCountry]);
const placeholder = useMemo(() => {
  if (phoneCountryFormat) {
    const words = phoneCountryFormat.split(' ');
    words.shift(); // I'm using only local numbers so here I remove the country code from the format
    let text = words.join(' ');
     // first digit is special on french numbers, these 3 lines could be removed
    if (country === 'fr') {
      text = text.replace('.', '6');
    }
    while (text.indexOf('.') > -1) {
      text = text.replace('.', `${Math.min(9, Math.max(1, Math.floor(Math.random() * 10)))}`);
    }
    return text;
  }
  return '';
}, [phoneCountryFormat, country]);

CDN

<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

GitHub license

Based on react-phone-input

Make sure you donated for lib maintenance Donate

@sheerid/jslib-nightly@borderfreefinancial/elements-react@secondcloset/web-components@avocad0/sdkreact-mindeevlk-mob-app@gamiphy/dashboard-corecirplus-frontend@mindee/react-web-essentialsemerson-dynamic-ui@wishi_fashion/web-chat-sdk-temp@devapi/design-systemnew-productornew-productor-v2@kiva/ssirius-standaloneobss-reactgrossjsmultisys-design-systemhfncomponents@gourban/ui-components@infinitebrahmanuniverse/nolb-react-phcompanyon-form-builderpreplaced-official-liblawb-fe@everything-registry/sub-chunk-2603coditas-uitechnoboot-ssocompanyon-formbuilderorbital_common_componentsorbital_new_common_componentsomo-test-packagepapel-uip4f-ds-webtestep4f-ds-webteste2p4f-ds-webp4f-ds-webtods-component-libpackrit-ui-librarynamirasoft-site-reactmyinvisalignmyhealthcaremyhealthcarehispatient_management@devforth/adminjs-design-system@demoeng/usecase-customer-opt-inzkteco-team-v2zkteco-team-v3zkteco-team-v4zkteco-team-v5atecl-ui-components-v3@chakadev/reactbnpl-wipeon@config.one/authc-b-t-demobr-core-webccw-frontend-componentcb-formcontrolscasting-shared-ui-componentscap-creatives-ui@gamiphy/react-app@worqapp/web-rnwaverunner-signup-form@berlitz/phone-input2@berlitz/input-v2automedsys-reusable-ui-modules@awake-travel/ui@ballerine/ui@bonitour/components@deepanshugolu/elements-reactbeeonis-ui-kit@ashit21121/avocado-sdk@axa-ch/aletheia@getkevin.eu/kevin-uiabdulazimadmin-core-componentswaleed-avocadowaleed-avocado-2vvdev-admin-panel-ui@ye-lib/components@yesness/react-inputwao-react-shared-componentsantd-styled@potok/forms@scpl/workcamp-ui@skovoroda/todocrud-libucentral-libs@akkurateio/forms@adminjs/design-system@angle-technologies/react-components@uniiverse/library@polichat/flamboyant@regen-network/web-componentsunivalley-os@tawol-tech/trogen@top-message/ui-libclientbase-library@groksmith/mychoice-sdk-componentscollab-test-cmp@gratify-eu/cartcommon-fields
2.15.1

2 years ago

2.15.0

2 years ago

2.14.0

3 years ago

2.13.9

3 years ago

2.13.8

4 years ago

2.13.7

4 years ago

2.13.6

4 years ago

2.13.5

4 years ago

2.13.4

4 years ago

2.13.3

4 years ago

2.13.2

4 years ago

2.13.1

4 years ago

2.13.0

4 years ago

2.12.1

4 years ago

2.12.0

4 years ago

2.11.0

4 years ago

2.10.1

4 years ago

2.10.0

5 years ago

2.9.5

5 years ago

2.9.4

5 years ago

2.9.3

5 years ago

2.9.2

5 years ago

2.9.1

5 years ago

2.9.0

5 years ago

2.8.0

5 years ago

2.7.1

5 years ago

2.7.0

5 years ago

2.6.1

6 years ago

2.6.0

6 years ago

2.5.1

6 years ago

2.5.0

6 years ago

2.4.1

6 years ago

2.4.0

6 years ago

2.3.0

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.1

7 years ago

2.0.0

7 years ago