Convert to E164 only if possible?
Asked Answered
L

2

12

Can I determine if the user entered a phone number that can be safely formatted into E164?

For Germany, this requires that the user started his entry with a local area code. For example, 123456 may be a subscriber number in his city, but it cannot be formatted into E164, because we don't know his local area code. Then I would like to keep the entry as it is. In contrast, the input 089123456 is independent of the area code and could be formatted into E164, because we know he's from Germany and we could convert this into +4989123456.

Legalese answered 27/1, 2014 at 6:55 Comment(0)
K
12

You can simply convert your number into E164 using libphonenumber and after conversion checks if both the strings are same or not. If they're same means a number can not be formatted, otherwise the number you'll get from library will be formatted in E164.

Here's how you can convert

PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
String formattedNumber = phoneUtil.format(inputNumber, PhoneNumberFormat.E164);

Finally compare formattedNumber with inputNumber

Kiefer answered 7/9, 2014 at 16:2 Comment(1)
I don't think this is correct, or it at least misunderstands the question.Bereft
B
1

It looks as though you'll need to play with isValidNumber and isPossibleNumber for your case. format is certainly not guaranteed to give you something actually dialable, see the javadocs. This is suggested by the demo as well, where formatting is not displayed when isValidNumber is false.

I also am dealing with this FWIW. In the context of US numbers: The issue is I'd like to parse using isPossibleNumber in order to be as lenient as possible, and store the number in E164. However then we accept, e.g. +15551212. This string itself even passes isPossibleNumber despite clearly (I think) not being dialable anywhere.

Bereft answered 17/1, 2017 at 19:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.