MobileNumberFormat – parse and validate mobile phone numbers

MobileNumberFormat parses and validates mobile phone numbers. It relies on the data available in Google’s libphonenumber library.

The library ex_phone_number already provides phone parsing/validation based on libphonenumber, but I wanted to create a smaller library specializing only on mobile numbers.

This is especially useful when you need to send text messages/SMS through SMS gateways.

iex> MobileNumberFormat.parse(%{country_calling_code: 32, national_number: "0455/12.34.56"})

{:ok,
 %{
   country_calling_code: "32",
   iso_country_code: "BE",
   national_number: "455123456",
   trimmed_national_number_input: "455/12.34.56"
 }}

iex> MobileNumberFormat.parse(%{country_calling_code: 32, national_number: "0455/12.34.56 111111"})

:invalid_number

iex> MobileNumberFormat.parse(%{international_number: "+32 455/12.34.56"})

{:ok,
 %{
   country_calling_code: "32",
   iso_country_code: "BE",
   national_number: "455123456",
   trimmed_national_number_input: "455/12.34.56"
 }}

iex> MobileNumberFormat.formatting_data_per_territory()

[
  # ...
  %{
    country_calling_code: "1",
    example: "4734031234",
    iso_country_code: "GD",
    national_prefix: "1",
    possible_lengths: [10],
    regex: ~r/473(?:4(?:0[2-79]|1[04-9]|2[0-5]|58)|5(?:2[01]|3[3-8])|901)\d{4}/
  },
  %{
    country_calling_code: "1",
    example: "8092345678",
    iso_country_code: "DO",
    national_prefix: "1",
    possible_lengths: [10],
    regex: ~r/8[024]9[2-9]\d{6}/
  },
  # ...
]
5 Likes