I need to validate only the country code extension in a phone number using Java. The validation should ensure that:
The country code starts with a valid + (e.g., +1, +94, +44).
The format is correct, rejecting cases like missing +, multiple + signs, or extra digits in the country code.
The validation should be performed using a reliable library to ensure accuracy.
I found a solution using Google's libphonenumber library to validate only the country code extension with the getRegionCodeForCountryCode method. However, some country code extensions cannot be identified, such as +876 (Jamaica's country code).
Is there a better or simpler way to validate country code extensions in a phone number in Java?
Example test cases:
+94 -> valid
+876 - > valid
I need to validate only the country code extension in a phone number using Java. The validation should ensure that:
The country code starts with a valid + (e.g., +1, +94, +44).
The format is correct, rejecting cases like missing +, multiple + signs, or extra digits in the country code.
The validation should be performed using a reliable library to ensure accuracy.
I found a solution using Google's libphonenumber library to validate only the country code extension with the getRegionCodeForCountryCode method. However, some country code extensions cannot be identified, such as +876 (Jamaica's country code).
Is there a better or simpler way to validate country code extensions in a phone number in Java?
Example test cases:
Share Improve this question edited Mar 25 at 7:10 M. Deinum 126k22 gold badges233 silver badges249 bronze badges asked Mar 25 at 3:30 chathurachathura 1051 silver badge9 bronze badges 3+94 -> valid
+876 - > valid
- 2 jamaica's telephone country code seems to be 1876 not 876 though. Can you show sample code with output that shows that the country code cannot be identified? – pebble unit Commented Mar 25 at 3:37
- 1 876 seems to be the area code, not country code. – pebble unit Commented Mar 25 at 3:40
- 4 Right. Jamaica is part of the North American numbering plan, which uses +1. 876 is the area code, like 212 for New York and 213 for Los Angeles. – Tim Roberts Commented Mar 25 at 3:41
1 Answer
Reset to default -2You can use the library's PhoneNumberUtil class which I found online -https://javadoc.io/doc/com.googlecode.libphonenumber/libphonenumber/8.4.1/com/google/i18n/phonenumbers/PhoneNumberUtil.html.
You can parse the phone number to find the country code, then check if it exists in the list of existing country codes given by the library.
Here is the code :-
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import java.util.Set;
public class ValidateCountryCode {
public static boolean isValidCode(String phoneNumber) {
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
// Optional
if (!phoneNumber.startsWith("+")) {
return false;
}
try {
// Parse number not assuming default location
Phonenumber.PhoneNumber number = phoneNumberUtil.parse(phoneNumber, null);
int countryCode = number.getCountryCode();
// Get set of all country codes
Set<Integer> countryCodes = phoneNumberUtil.getSupportedCallingCodes();
return countryCodes.contains(countryCode);
} catch (NumberParseException e) {
return false;
}
}
public static void main(String[] args) {
System.out.println(isValidCode("+94")); // true (Sri Lanka)
System.out.println(isValidCode("+876")); // true (Jamaica)
System.out.println(isValidCode("+999")); // false (Invalid)
System.out.println(isValidCode("876")); // false (Missing +)
System.out.println(isValidCode("++94")); // false (Multiple +)
}
}