[JAVA] Phone number input check

is.

There is a 5-digit area code, a 4-digit-3 digit-3 digit telephone number, etc. It's a hassle if you really try to check the phone number input on your own.

Let's leave it to the library.

pom.xml


<!-- https://mvnrepository.com/artifact/com.googlecode.libphonenumber/libphonenumber -->
<dependency>
    <groupId>com.googlecode.libphonenumber</groupId>
    <artifactId>libphonenumber</artifactId>
    <version>8.8.5</version>
</dependency>
final PhoneNumberUtil PHONE_NUMBER_UTIL = PhoneNumberUtil.getInstance();

String tel = "045-1230-4567";//true
//String tel = "04505-2-4567";//true
//String tel = "0454-130-467";//true
//String tel = "0454-130-499967";//false
//The second argument specifies which country's phone number the tel variable is. com.google.i18n.phonenumbers.Select the one written in CountryCodeToRegionCodeMap.
PhoneNumber phoneNumber = PHONE_NUMBER_UTIL.parse(tel, "JP");
boolean result= PHONE_NUMBER_UTIL.isValidNumber(phoneNumber);
System.out.println(result);

Recommended Posts

Phone number input check
Easy input check with Bean Validation!
[Java] Check the number of occurrences of characters
Not "immediate = true equal input check avoidance"