EctoPhone

An ecto type for phone numbers.

Installation

  {:ecto_phone "~> 1.0"}

Usage

EctoPhone may be used in place of :string fields, where extra parsing and validation is desired.

defmodule MySchema do
  use Ecto.Schema
  
  schema "table_name" do
    field :phone, EctoPhone
    field :phone, EctoPhone, default_prefix: 1
    field :phone, EctoPhone, format: :national
  end
end

Phone formats accept formats compatible with the ex_phone_number library, and are used in the EctoPhone implementations of String.Chars and Phoenix.HTML.Safe:

EctoPhone also provides the ~PHONE sigil for more concise creation of phone structs.

iex> import EctoPhone, only: [sigil_PHONE: 2]
...>
iex> ~PHONE[1 415 555 5555]i
%EctoPhone{e164: 14155555555, format: :international}

Configuration

EctoPhone may be configured at compile time with the following values:

config :ecto_phone,
    default_prefix: 1,
    default_format: :international

Alternatives