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
PHONE_VALID(phone, country_code?)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)
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)
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)
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.
Last updated
Was this helpful?
