Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert to E164 only if possible?

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.

like image 849
Norbert Avatar asked Jan 27 '14 06:01

Norbert


People also ask

What is an e164 number?

E. 164 is the international telephone numbering plan that ensures each device on the PSTN has globally unique number. This number allows phone calls and text messages can be correctly routed to individual phones in different countries. E.


2 Answers

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

like image 119
Hussain Mansoor Avatar answered Oct 16 '22 09:10

Hussain Mansoor


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.

like image 40
jberryman Avatar answered Oct 16 '22 09:10

jberryman