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.