Skip to contents
  • phone_possible() checks length only (matching libphonenumber's isPossibleNumber()): whether the number has a length that exists for its country calling code, including local-only numbers.

  • phone_valid() checks the full numbering plan (matching libphonenumber's isValidNumber()): country calling code, prefix digits and length.

A TRUE from phone_valid() means the number is well-formed under that country's numbering plan. It does not mean the number was assigned or is active.

Usage

phone_valid(x, region = NULL, strict = FALSE)

phone_possible(x, region = NULL)

Arguments

x

A character vector of phone numbers (or a dialtone_phone data frame from phone_parse()).

region

A single two-letter CLDR region code (such as "US", "GB" or "IN") giving the default country for numbers written without a leading +, or NULL when numbers carry an international + prefix.

strict

Logical scalar. When FALSE (the default), region is the default region used to parse national-format strings, and any valid international number returns TRUE. When TRUE, region must be non-null and the number must also belong to region (matching isValidNumberForRegion()).

Value

A logical vector the same length as x: TRUE, FALSE, or NA where x is NA. Malformed values return FALSE, never an error.

Examples

phone_possible(c("+1 650-253-0000", "+1 111-111-1111", "123", NA))
#> [1]  TRUE  TRUE FALSE    NA
phone_valid(c("+1 650-253-0000", "+1 111-111-1111", "123", NA))
#> [1]  TRUE FALSE FALSE    NA

# National format requires a default region
phone_valid(c("020 7946 0192", "020 0000 0000"), region = "GB")
#> [1] TRUE TRUE

# strict = TRUE requires the number to belong to `region`
phone_valid(c("+1 650-253-0000", "+1 416-555-0199"), region = "US", strict = TRUE)
#> [1]  TRUE FALSE