International phone number without a plus sign parsed invalid
See original GitHub issueformatInternational() doubles the country code. It’s a common case to parse a phone without a plus sign. For exampl, sms gateways accept both variants: with or without a plus, also many apps exports phones without a plus (yes, it’s completely incorrect, but this happens everywhere). Can it be handled automatically or should a person decide by himself add a plus or not, even when the country code is specified?
parsePhoneNumberFromString('375447521111', 'BY')
PhoneNumber
country: "BY"
countryCallingCode: "375"
metadata: {version: "1.7.40", country_calling_codes: Object, countries: Object, nonGeographic: Object}
nationalNumber: "375447521111"
number: "+375375447521111"
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Formatting International Phone Numbers - Twilio Support
164 phone number formatting entails the following: A + (plus) sign (replacing the International Call Prefix like 011); International Country Calling code; Local ......
Read more >4.3. Validate International Phone Numbers - O'Reilly
You want to validate international phone numbers. The numbers should start with a plus sign, followed by the country code and national number....
Read more >Getting INVALID_COUNTRY_CODE exception when trying to ...
I saw following code as recommended. PhoneNumber phoneNumber = phoneUtil.parse(numberStr, ""));. However, this is throwing INVALID_COUNTRY_CODE ...
Read more >Using libphonenumber for International Phone Numbers
Libphonenumber is Google's formatting, parsing, and validation tool for international phone numbers. Learn how to use it in your global apps ...
Read more >How to write a cell phone number in an international way ...
+: Plus sign ; country code: International country code. It comes after the plus sign. For ex. It is +91 for India whereas...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Works like a charm!
@testarossaaaaa Hmm, actually that’s a valid correction. Isn’t the
isValid()
check too strict though?isValid()
returnstrue
if the number matches the exact regular expressions for the country. For example, for mobile numbers in France it’s700\d{6}|(?:6\d|7[3-9])\d{7}
. There’s also another way just to validate phone number length:isPossible()
. For France it’spossible_lengths: [9]
. For Germany, it’spossible_lengths: [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
, soisPossible()
check wouldn’t be so reliable for Germany (+49
), because it could be a “possible” 10-digit “local” number, or it could be a “possible” 8-digit “local” number plus the49
country calling code prepended to it, so justisPossible()
wouldn’t tell those two apart (49
is a valid “area code” in Germany).isValid()
is more precise, but that’s only if you’re looking for only “valid” numbers, dismissing all other “possible” numbers.How does Google behave in this scenario? https://github.com/google/libphonenumber/blob/master/FAQ.md#why-wasnt-the-country-code-removed-when-parsing Seems that they do an
isPossible()
check.Source: https://github.com/google/libphonenumber/blob/876268eb1ad6cdc1b7b5bef17fc5e43052702d57/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L2738-L2752
Source: https://github.com/google/libphonenumber/blob/876268eb1ad6cdc1b7b5bef17fc5e43052702d57/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L2806-L2831
So, first they check whether a non-international number starts with the default country’s calling code. If it does, they temporarily construct a shortened number for it. Then they choose that shortened number over the initial number if:
"national_number_pattern": "[1-9]\\d{8}"
) doesn’t match the initial number but does match the shortened number.possible_lengths
for the country).Seems reasonable. In fact, I guess I’ll make it part of the library. Maybe some time later today.