defmodule Fields.Validate do @moduledoc """ Helper functions to validate the data in certain fields """ @doc """ Validate an address. Currently just validates that some input has been given. """ def address(address) do String.length(address) > 0 end @doc """ Validate the format of an email address using a regex. Uses a slightly modified version of the w3c HTML5 spec email regex (https://www.w3.org/TR/html5/forms.html#valid-e-mail-address), with additions to account for not allowing emails to start or end with '.', and a check that there are no consecutive '.'s. """ def email(email) do {:ok, regex} = Regex.compile( "^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]*(?