International phone number input for React Native with country picker supporting 235 countries. Uses ruimarinho/google-libphonenumber to validate input and automatically translates to valid e164 format.
This repo is a fork of @sesamsolutions/phone-input. Notable changes:
- Improved autofill support for iOS and Android
- Phone number is formatted as you type
- Added support for
testIDprop for automated testing (e.g. with Jest) - Added support for
textInputPropsprop to pass any additional props to the underlyingTextInputcomponent
npm install github:thefirstquestion/react-native-phone-inputimport PhoneInput from "@thefirstquestion/react-native-phone-input";import React from 'react'
import { Alert, View } from 'react-native'
import import PhoneInput from "@thefirstquestion/react-native-phone-input";
const App = () => {
const handleChange = (data) => {
if (data.isValid) {
Alert.alert(data.e164)
} else {
console.log(data.input + ' is not a valid phone number')
}
}
return (
<View style={{ alignItems: 'center', flex: 1, justifyContent: 'center' }}>
<PhoneInput
initialCountry="US"
onChange={handleChange} />
</View>
)
}
export default AppSet the initial country and dial code. Defaults to US.
Returns an object containing information about the Phone number every time the input or value changes.
countryCode: currently selected country code.nullif not selected.dialCode: current dial code.nullif not selected.e164: e164 valid phone number.nullif invalid.input: raw phone number input. Same asonChangePhoneNumber.isValid:booleanindicating if input is a valid phone number.
Returns current raw input string. Use onChange instead to benefit from additional validations.
Changes the current input. Be careful not to update this with onChange. This will cause an endless loop.
Hide the keyboard automatically when the input is considered valid e164 format. Defaults to true.
Customize the phone number input.
Customize text inside the phone number input.
Boolean indicating if the input should be focused when the component is mounted. Defaults to false.
Used to locate the phone number input for testing purposes.
The parent View has testID {testID}-container, the flag button has testID {testID}-flag-button, and the TextInput has testID {testID}-input.
Called when the input is focused.
Called when the input is blurred, i.e. when the input loses focus.
Pass any additional props to the underlying TextInput component.
Disables the country code selector and hides the flag icon. Defaults to false.
Run npm run example to start the example app.
This library was bootstrapped using create-react-native-library.