Title: Phone Number Validation Using Google's 'libphonenumber' Metadata
Version: 0.1.0
Description: Parses, validates, formats, and classifies phone numbers using Google's 'libphonenumber' metadata. Covers 240+ territories with support for mobile, landline, toll-free, and other number types. Unlike 'dialr', this package requires no Java runtime — metadata is parsed ahead of time from the upstream 'PhoneNumberMetadata.xml' and shipped as a bundled R object. Functions accept character vectors and return simple R types (logical, character, data.frame).
License: Apache License (≥ 2)
URL: https://pcstrategyandopsco.github.io/dialvalidator/, https://github.com/pcstrategyandopsco/dialvalidator
BugReports: https://github.com/pcstrategyandopsco/dialvalidator/issues
Encoding: UTF-8
RoxygenNote: 7.3.3
Imports: cli, rlang, stringr
Suggests: xml2, testthat (≥ 3.0.0), knitr, rmarkdown
Depends: R (≥ 3.5.0)
Config/testthat/edition: 3
VignetteBuilder: knitr
NeedsCompilation: no
Packaged: 2026-03-09 09:49:32 UTC; peeyooshchandra
Author: Peeyoosh Chandra [aut, cre]
Maintainer: Peeyoosh Chandra <pc@strategyandops.co>
Repository: CRAN
Date/Publication: 2026-03-12 19:50:08 UTC

dialvalidator: Phone Number Validation Using Google's 'libphonenumber' Metadata

Description

logo

Parses, validates, formats, and classifies phone numbers using Google's 'libphonenumber' metadata. Covers 240+ territories with support for mobile, landline, toll-free, and other number types. Unlike 'dialr', this package requires no Java runtime — metadata is parsed ahead of time from the upstream 'PhoneNumberMetadata.xml' and shipped as a bundled R object. Functions accept character vectors and return simple R types (logical, character, data.frame).

Author(s)

Maintainer: Peeyoosh Chandra pc@strategyandops.co

See Also

Useful links:


Get the full metadata object

Description

Returns the parsed libphonenumber metadata used internally by all validation functions. Useful for inspection and debugging.

Usage

dv_metadata()

Value

A list with elements cc_to_regions, territories, example_numbers, version, and built.

Examples

meta <- dv_metadata()
meta$version
length(meta$territories)


Get metadata for a specific territory

Description

Get metadata for a specific territory

Usage

dv_territory(region)

Arguments

region

ISO 3166-1 alpha-2 region code (e.g., "NZ", "US").

Value

A list containing the territory's country code, national prefix, phone type patterns, formatting rules, and example numbers. Returns NULL if the region is not found.

Examples

nz <- dv_territory("NZ")
nz$country_code
nz$mobile$example


Update metadata from upstream

Description

Downloads the latest PhoneNumberMetadata.xml from Google's libphonenumber repository, parses it, and saves the result to the user's cache directory. The updated metadata is used for the remainder of the session.

Usage

dv_update_metadata(
  url = paste0("https://raw.githubusercontent.com/google/libphonenumber/master/",
    "resources/PhoneNumberMetadata.xml")
)

Arguments

url

URL to the raw PhoneNumberMetadata.xml file. Defaults to the master branch on GitHub.

Details

Requires the xml2 package.

Value

Invisibly returns the path to the cached metadata file.

Examples


dv_update_metadata()



Access Phone Number Metadata

Description

Functions to access and update the bundled libphonenumber metadata.


Detect Country of a Phone Number

Description

Returns the ISO 3166-1 alpha-2 region code for each phone number.

Usage

phone_country(number, default_region = NULL)

Arguments

number

Character vector of phone numbers.

default_region

ISO 3166-1 alpha-2 region code for numbers in national format. See phone_parse() for details.

Value

Character vector of region codes (e.g., "NZ", "US"). Returns NA for numbers that cannot be parsed.

Examples

phone_country("+64211234567")
phone_country(c("+12125551234", "+442071234567"))


Format Phone Numbers

Description

Formats phone numbers according to standard conventions.

Usage

phone_format(
  number,
  format = c("E164", "NATIONAL", "INTERNATIONAL"),
  default_region = NULL
)

Arguments

number

Character vector of phone numbers.

format

One of "E164" (default), "NATIONAL", or "INTERNATIONAL".

E164

+64211234567 — compact international format with no spaces.

NATIONAL

⁠021 123 4567⁠ — local dialling format with national prefix.

INTERNATIONAL

⁠+64 21 123 4567⁠ — international format with spaces.

default_region

ISO 3166-1 alpha-2 region code for numbers in national format. See phone_parse() for details.

Value

Character vector of formatted numbers. Returns NA for numbers that cannot be parsed.

Examples

phone_format("+64211234567")
phone_format("+64211234567", "NATIONAL")
phone_format("+64211234567", "INTERNATIONAL")


Get All Phone Number Information

Description

Parses, validates, formats, and classifies phone numbers in a single call. Returns a data frame with one row per input number.

Usage

phone_info(number, default_region = NULL)

Arguments

number

Character vector of phone numbers.

default_region

ISO 3166-1 alpha-2 region code for numbers in national format. See phone_parse() for details.

Value

A data frame with columns:

raw

Original input.

e164

E.164 formatted number.

national

National format.

international

International format.

region

ISO 3166-1 alpha-2 region code.

country_code

Country calling code.

type

Number type (mobile, fixed_line, etc.).

valid

Logical validation result.

Examples

phone_info("+64211234567")
phone_info(c("+64211234567", "+12125551234"))


Parse a Phone Number

Description

Parses a phone number string into its components: country code, national number, and region. Numbers can be provided in international format (with leading +) or national format (with default_region).

Usage

phone_parse(number, default_region = NULL)

Arguments

number

Character vector of phone numbers.

default_region

ISO 3166-1 alpha-2 region code (e.g., "NZ", "US") used when numbers are in national format (no leading +). If NULL, numbers without a + prefix will fail to parse.

Value

A list of parsed phone number lists, each with elements:

raw

The original input string.

country_code

Country calling code (e.g., "64" for NZ).

national_number

The national significant number (digits only).

region

ISO 3166-1 alpha-2 region code.

valid

Logical indicating if the number is valid.

Returns a list with valid = FALSE for numbers that cannot be parsed.

Examples

phone_parse("+64211234567")
phone_parse("021 123 4567", default_region = "NZ")
phone_parse(c("+12125551234", "+442071234567"))


Detect Phone Number Type

Description

Classifies phone numbers as mobile, fixed-line, toll-free, etc. by matching against libphonenumber's per-territory type patterns.

Usage

phone_type(number, default_region = NULL)

Arguments

number

Character vector of phone numbers.

default_region

ISO 3166-1 alpha-2 region code for numbers in national format. See phone_parse() for details.

Value

Character vector of phone number types. Possible values: "mobile", "fixed_line", "fixed_line_or_mobile", "toll_free", "premium_rate", "shared_cost", "personal_number", "voip", "pager", "uan", "voicemail", "unknown". Returns NA for numbers that cannot be parsed.

Examples

phone_type("+64211234567")
phone_type(c("+6493001234", "+64800123456"))


Validate Phone Numbers

Description

Checks whether phone numbers are valid according to libphonenumber's metadata. A number is valid if it parses successfully, has a valid length, and matches the general number pattern for its region.

Usage

phone_valid(number, default_region = NULL)

Arguments

number

Character vector of phone numbers.

default_region

ISO 3166-1 alpha-2 region code for numbers in national format. See phone_parse() for details.

Value

Logical vector. TRUE for valid numbers, FALSE otherwise. NA inputs return FALSE.

Examples

phone_valid("+64211234567")
phone_valid(c("+64211234567", "+6421", "not a number"))
phone_valid("021 123 4567", default_region = "NZ")