How can we validate the length of a mobile number by COUNTRY? (Knowing that the country code might or might not be part of the number)
Length may vary per country, meaning there should be a length range or pattern validator.
Example:
Note: Any idea where can I find each country's mobile number length range.
Thank you!
Given the mobile number and the country code, you can use libphonenumeer which is a Google library for validating phone numbers; it checks the number length and catches NumberParseException
exception if it is not a right number.
This is their sample in how to use it
String swissNumberStr = "044 668 18 00";
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
PhoneNumber swissNumberProto = phoneUtil.parse(swissNumberStr, "CH");
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}