例如,libphonenumber ++++++614- -12a345678(())&认为以下(无效)AU手机号码是有效的:

Tested in their demo site

java - 如何在libphonenumber(Java)中进行严格的电话号码验证?-LMLPHP

java - 如何在libphonenumber(Java)中进行严格的电话号码验证?-LMLPHP

明智的代码:

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);


在libphonenumber v8.9.2上测试:

compile group: 'com.googlecode.libphonenumber', name: 'libphonenumber', version: '8.9.2'

最佳答案

首先,编写一种使用正则表达式验证输入的方法。就像是:

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


也可以看看:
This post, phoneregex.comrexlib's section,

10-02 11:28