daddyz / phonelib

Ruby gem for phone validation and formatting using google libphonenumber library data
MIT License
1.06k stars 130 forks source link

international format fails on german mobile numbers #168

Open Shonyyyyy opened 5 years ago

Shonyyyyy commented 5 years ago

When parsing a german phone number string by calling Phonelib.parse().international the parsed number gets an extra country_code number.

E.g.: Phonelib.parse('491511234567').international returns +49 491 511234567. Expected would be: +49 151 1234567.

This only appears when the following is given:

danborden1 commented 5 years ago

In rails, this behavior can be fixed by adding Phonelib.strict_double_prefix_check = true to config/initializers/phonelib.rb

daddyz commented 5 years ago

@Shonyyyyy number 491511234567 is not valid by google's regex, their regex says that there should be 8 symbols after 151.

Phonelib.parse('49 151 12345678').international # => "+49 1511 2345678"

Since number is not valid, for countries with option to have double country prefix, library tries to add additional country prefix and reparse, and in case it's valid, the new number will be returned. So suggestion offered by @chiefjuss is not good.

 Phonelib.parse('+49 151 1234567').valid? # => false

In case 7 digits numbers are valid, you need to open an issue in original google lib

Shonyyyyy commented 5 years ago

ok, thank you very much so far. I will try out that strict_double_prefix_check or try to use the validation properly before saving. I will let you know.

krtschmr commented 5 years ago

wiki says

https://en.wikipedia.org/wiki/Telephone_numbers_in_Germany

015xx-xxxxxxx, 016x-xxxxxxx, 017x-xxxxxxx Mobile numbers are assigned non-geographic area codes starting with 015, 016 and 017 and have a length of 3 or 4 digits without the trunk prefix. The numbers have a total length of ten or eleven digits without the trunk prefix: numbers starting with 17 or 16 are 10 digits long except 176 and 1609, which are 11 digits long like the numbers starting with 15. Network operators issue area codes as listed in the following table.

so i assume 151 1234567 has a lenght of 10 and should be valid? guess google is doing wrong?

daddyz commented 5 years ago

@krtschmr As I saw they are not accepting wiki as official data source, they require links to some authoritative source. Note that prefix 015xx is written with 2 x while 016x and 017x has only 1. I am not sure if it is valid or not, but anyway issue should be raised for them in case this is an error. If I am adding additional number for 015 it works:

Phonelib.parse('4915111234567').international # => "+49 1511 1234567"

Currently google library still returns +49 491 511234567 while parsing 491511234567