dongri / phonenumber

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

UK Telephone Numbers #45

Closed ainsleyclark closed 6 days ago

ainsleyclark commented 2 weeks ago

It's common for the UK mobiles to start with 07. For example: 07933846223

But it seems that Gabon already has this.

i.Alpha2 = "GA"
i.Alpha3 = "GAB"
i.CountryCode = "241"
i.CountryName = "Gabon"
i.MobileBeginWith = []string{"05", "06", "07"}
i.PhoneNumberLengths = []int{8}
iso3166Datas = append(iso3166Datas, i)

i.Alpha2 = "GB"
i.Alpha3 = "GBR"
i.CountryCode = "44"
i.CountryName = "United Kingdom"
i.MobileBeginWith = []string{"7"}
i.PhoneNumberLengths = []int{10}
iso3166Datas = append(iso3166Datas, i)

This results in default country code 17933846223

dongri commented 2 weeks ago

@ainsleyclark Which function did you call, and what were the results?

ainsleyclark commented 2 weeks ago

Hi @dongri

func FormatTelephone(number string) (string, error) {
    if number == "" {
        return "", errors.New("telephone cannot be empty")
    }
    c := phonenumber.GetISO3166ByNumber(number, true)
    tel := phonenumber.ParseWithLandLine(number, c.Alpha3)
    return formatInternal(number, tel)
}
dongri commented 2 weeks ago

@ainsleyclark This library provides several features, but it seems that GetISO3166ByNumber does not work well with landline numbers.
You might want to write it like this:

tel := phonenumber.ParseWithLandLine("07933846223", "GB")
ainsleyclark commented 2 weeks ago

Thanks @dongri

I don't have access the callers country though, just the number. Is there another workaround?

Thanks again.

dongri commented 1 week ago

I added a function to retrieve the country from a mobile number. https://github.com/dongri/phonenumber/blob/main/examples/main.go#L10 Can use v0.1.7

ainsleyclark commented 1 week ago

Thanks for your reply.

If I don't know if it's a mobile or landline what would you suggest?

dongri commented 1 week ago

If there is no country code attached, I think it would be difficult to determine the number with just a landline.

ainsleyclark commented 1 week ago

Thanks for your reply, is there a way to determine if a given input is a mobile number?

dongri commented 1 week ago

As far as I know, if there is no country code, I think it is impossible to determine.