SmsUp.Numbers.PhoneValidator (sms_up v1.4.1)

View Source

This module provides function to validate phone number formats.

Summary

Functions

Format the given number to international format.

Check if the number seems to be valid format wise.

Functions

format(number, country_code)

@spec format(String.t(), String.t()) :: {:ok, String.t()} | {:error, String.t()}

Format the given number to international format.

Returns a ok tupple with the formated number or an erro tupple and the reason.

Examples

iex> SmsUp.Numbers.PhoneValidator.format("+41765556677", "CH")
{:ok, "+41765556677"}

iex> SmsUp.Numbers.PhoneValidator.format("0765556677", "CH")
{:ok, "+41765556677"}

iex> SmsUp.Numbers.PhoneValidator.format("765556677", "CH")
{:ok, "+41765556677"}

iex> SmsUp.Numbers.PhoneValidator.format("0630772288", "FR")
{:ok, "+33630772288"}

iex> SmsUp.Numbers.PhoneValidator.format("0630772288", "ZZ")
{:error, "Invalid country calling code"}

iex> SmsUp.Numbers.PhoneValidator.format("Hello", "FR")
{:error, "The string supplied did not seem to be a phone number"}

is_valid_number?(number)

@spec is_valid_number?(String.t()) :: true | false

Check if the number seems to be valid format wise.

Returns true or false.

Examples

iex> SmsUp.Numbers.PhoneValidator.is_valid_number?("+41765556677")
true

iex> SmsUp.Numbers.PhoneValidator.is_valid_number?("0765556677")
false

iex> SmsUp.Numbers.PhoneValidator.is_valid_number?("555")
false

iex> SmsUp.Numbers.PhoneValidator.is_valid_number?("hello")
false