skip to Main Content
<PhoneInput
        ref={phoneInput}
        defaultValue={phoneNumber}
        defaultCode="GB"
        layout="second"
        withShadow
        filterProps
        autoFocus
        disableArrowIcon
        containerStyle={styles.phoneContainer}
        textContainerStyle={styles.textInput}
        onChangeFormattedText={(text) => {
          setphoneNumber(text);
        }}
      />

I am using the library ‘react-native-phone-number-input’ so a user can easily enter their number with the country code already there. Right now our app only allows users from the UK so I was wondering if there is a way to only have the ‘GB’ country code with no other country codes available?

I disabled the arrow icon but the user is still able to click on the country code and then given the option to select another.

2

Answers


  1. Hey you can do via passing countryPickerProps as.

    <PhoneInput
            ref={phoneInput}
            defaultValue={phoneNumber}
            defaultCode="GB"
            layout="second"
            withShadow
            filterProps
            autoFocus
            disableArrowIcon
            containerStyle={styles.phoneContainer}
            textContainerStyle={styles.textInput}
            onChangeFormattedText={(text) => {
              setphoneNumber(text);
            }}
            countryPickerProps={{
              countryCodes: ['GB'],
            }}
          />
    
    Login or Signup to reply.
  2. I think you can create state for country code and set "GB" as default country code and pass this state to "defaultCode" and you can may be write the following code in "onChangeCountry" prop,

    onChangeCountry = {(country)=>{
         if(country !== "GB"){
              //you can show toast or error message whatever you like
              console.log('---our service is currently only available in UK');
              return;
         }
         //set the state of country code
         setCountryCode(country)
    }}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search