1

For example, the following (invalid) AU mobile phone number is considered valid by libphonenumber ++++++614- -12a345678(())&:

Tested in their demo site:

https://libphonenumber.appspot.com

Response

Code wise:

final String mobilePhoneNumber = "++++++614-  -12a345678(())&";
final String region = "AU";
final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();

// true
// I don't get it, how come ++++++614-  -12a345678(())& is even considered a possible number??
System.out.println(phoneNumberUtil.isPossibleNumber(mobilePhoneNumber, region));

final Phonenumber.PhoneNumber phoneNumber = phoneNumberUtil.parse(mobilePhoneNumber, region);
// true
final boolean validNumberForRegion = phoneNumberUtil.isValidNumberForRegion(phoneNumber, region);
// true
final boolean validMobileNumber = phoneNumberUtil.getNumberType(phoneNumber).equals(PhoneNumberType.MOBILE);

Tested on the libphonenumber v8.9.2:

compile group: 'com.googlecode.libphonenumber', name: 'libphonenumber', version: '8.9.2'
Yudhistira Arya
  • 3,491
  • 6
  • 25
  • 42

2 Answers2

2

I know it's already been two years, but if someone is looking for an answer, then please consider looking here, as said upper. Between texts and tables, you'll find eventually this one:

enter image description here

Better said, the phone number is correct, but not the format. And thus, the one we need is the generally either the international or the national format plus region or country code.

For example:

final String mobilePhoneNumber = "++++++614-  -12a345678(())&";
final String region = "AU";
final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
final Phonenumber.PhoneNumber phoneNumber = phoneNumberUtil.parse(mobilePhoneNumber, region);

String formatted = phoneNumberUtil.format(number, PhoneNumberFormat.INTERNATIONAL);

String national = phoneNumberUtil.format(number, PhoneNumberFormat.NATIONAL);

System.out.println(formatted); // +61 412 345 678
System.out.println(national); // 0412 345 678

Selast Lambou
  • 708
  • 12
  • 27
0

For one, write a method to verify the input using regex. Something like:

boolean validPhone(String str){
str.matches("^[+]?\\d{2,4}(-?\\d{1,5}){0,2}$");
}

see also: This post, phoneregex.com, and rexlib's section,

Stav Noy
  • 419
  • 4
  • 14
  • 4
    OP specifically asked for examples in Google library **libphonenumber** (Java). You are reinventing the wheel. Your function does not handle checking for actually valid area codes in USA or if it is a valid Italian telephone number (with or without country code?) or many many other use cases that **libphonenumber** handles. Sorry to be so negative, but this is not a helpful answer. – m1m1k Mar 27 '19 at 16:41