Skip to content

Instantly share code, notes, and snippets.

@neara
Forked from mgamini/Elixir Email Validation
Created February 17, 2020 23:05
Show Gist options
  • Save neara/8e5cca75633a7ee9a3e601bcf9e09d27 to your computer and use it in GitHub Desktop.
Save neara/8e5cca75633a7ee9a3e601bcf9e09d27 to your computer and use it in GitHub Desktop.
Elixir Email Validation
defmodule EmailValidator do
# ensure that the email looks valid
def validate_email(email) when is_binary(email) do
case Regex.run(~r/^[\w.!#$%&’*+\-\/=?\^`{|}~]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*$/i, email) do
nil ->
{:error, "Invalid email"}
[email] ->
try do
Regex.run(~r/(\w+)@([\w.]+)/, email) |> validate_email
rescue
_ -> {:error, "Invalid email"}
end
end
end
# check the email against a list of accepted domains, then make check if it is unique
def validate_email([email, username, host]) do
case host in Config.accepted_domains do
true ->
case find_by_email(email) do
nil -> :ok
_account -> :account
end
_ ->
{:error, "Not an accepted domain."}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment