hbb20 / CountryCodePickerProject

Country Code Picker (CCP) is an android library which provides an easy way to search and select country or international phone code. Also supports Android EditText phone mask and international phone validation.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

getFullNumberWithPlus() includes national trunk prefixes

Nilhcem opened this issue · comments

The CountryCodePicker.getFullNumberWithPlus() method appends a '+' to the countryCode and carrierNumber from user input.

When a user is inputting a phone number including the national trunk prefix, for instance for French (+33) numbers the value '0610101010', the method getFullNumberWithPlus() returns +330610101010 instead of +33610101010.

The first '0' directly after the country code is only used for national calls. International calls with '+33' should remove it.

Hence, the getFullNumberWithPlus() method can return invalid phone numbers.
Here's an example of I expected the getFullNumberWithPlus() to work, using the embedded libphonenumber-android lib:

private fun getExpectedFullNumberWithPlus(context: Context): String {
  val phoneUtil = PhoneNumberUtil.createInstance(context)
  try {
    val countryCode = countryCodePicker.selectedCountryCode
    val defaultRegion = countryCodePicker.selectedCountryNameCode

    var numberToParse = countryCodePicker.fullNumber
    if (numberToParse.startsWith(countryCode)) {
      numberToParse = numberToParse.substring(countryCode.length)
    }

    val phoneNumber = phoneUtil.parse(numberToParse, defaultRegion)
    return "+${phoneNumber.countryCode}${phoneNumber.nationalNumber}"
  } catch (e: NumberFormatException) {
    Timber.e(e, "Error parsing number")
  }
  return countryCodePicker.fullNumberWithPlus
}

Thank you for the suggestion. This has been fixed in version 2.1.4.