Convert to E164 only if possible?
libphonenumber
Solution
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
Problem
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`.