PHONE_VALID¶
Description¶
PHONE_VALID checks whether a string is a valid phone number using Google's
libphonenumber.
It performs two checks in sequence:
- Possible number, is the length plausible for any phone number in that country?
- Valid number, does the number match a known number range or pattern for that country?
A number that passes the length check but does not match a known range returns false.
A number that cannot be parsed at all returns unknown.
The input string may contain spaces, dashes, or parentheses, these are handled by the parser.
A leading 00 is treated as an international dialling prefix, equivalent to +.
If the number does not start with + or 00, a country_code must be provided to interpret it as a local number.
Without one, the result is unknown.
Syntax¶
Returns: true if the number is valid, false if it is structurally invalid, unknown if the input cannot be
parsed (e.g. empty string, unrecognised country code, no country context for a local number).
What counts as valid?¶
libphonenumber validates both number length and whether the number falls within a known range for the country. This means:
+41791234567âtrue: valid Swiss mobile number+41791234âfalse: too short to be a valid Swiss number+41999999999âfalse: correct length but not a valid Swiss number range0041123âfalse: the00prefix is treated as+, making it+41123, which is too short+99999999999âunknown: unrecognised country calling code; the parser throws a parse exception rather than returning a structural result
Examples, Switzerland (CH, +41)¶
Returns true, valid Swiss mobile number in international format.
Returns true, 00 is treated as +, equivalent to +41791234567.
Returns true, valid Swiss mobile number in local format.
Returns false, too short to be a valid Swiss number (only 7 digits after the country code, Swiss numbers require 9).
Returns false, length is plausible, but the number does not fall within any known Swiss number range.
Returns false, too short.
Examples, Germany (DE, +49)¶
Returns true, valid German mobile number (Telekom range).
Returns true, valid German mobile number in local format.
Returns true, valid Berlin landline.
Returns false, too short.
Returns false, does not match a known German number range.
Examples, United Kingdom (GB, +44)¶
Returns true, valid UK mobile number.
Returns true, valid UK mobile number in local format.
Returns true, valid London landline.
Returns false, too short.
Returns false, does not match a known UK number range.
Edge cases¶
Returns unknown, 'XX' is not a recognised country code.
Returns unknown, no country context to interpret the local number.
Returns unknown, +999 is not a recognised country calling code, so the parser throws a parse exception and cannot
return a structural result.
Example in context¶
Returns true if the entered number is a valid Swiss phone number or a valid internationally prefixed number.
Commonly used to validate phone inputs before allowing case progression.