dongri / phonenumber

With a given country and phone number, validate and format the MOBILE phone number to E.164 standard
MIT License
186 stars 46 forks source link

Invalid US number incorrectly identified as a Jamaica number #38

Closed abhatia1176 closed 2 months ago

abhatia1176 commented 10 months ago

The library incorrectly identifies an invalid USA phone number as Jamaica number.

Example: phonenumber.ParseWithLandLine("+1 289 2999", "US")

Expected output: "". Actual output: +12892999. I believe that since the length matched Jamaica's number length (7), it matched it with the country Jamaica.

The issue is that despite the country being provided correctly in the input, the function tries to identify the ISO3166 by Number in the following lines, and the GetISO3166ByNumber function matches with Jamaica. I did not follow why the library tried to retrieve ISO3166 by phone number when the correct country (US) was already provided as input.

    if plusSign {
        iso3166 = GetISO3166ByNumber(number, landLineInclude)
    } else {
        if indexOfInt(len(number), iso3166.PhoneNumberLengths) != -1 {
            number = iso3166.CountryCode + number
        }
    } 
andreyvit commented 8 months ago

From what I understand, and it's often the desirable behavior, the provided country is just a fallback for phone numbers not in an international format. If a phone number is a valid international phone number, you often don't want to reject it. Say you have a customer traveling from Jamaica, they have a valid phone number, you can serve them even in US.

dongri commented 2 months ago

@abhatia1176 @andreyvit Fixed. Please use v0.1.8