Guest User

Untitled

a guest
Mar 17th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. defmodule EmailValidator do
  2.  
  3. # ensure that the email looks valid
  4. def validate_email(email) when is_binary(email) do
  5. case Regex.run(~r/^[A-Za-z0-9._%+-+']+@[A-Za-z0-9.-]+\.[A-Za-z]+$/, email) do
  6. nil ->
  7. {:error, "Invalid email"}
  8. [email] ->
  9. try do
  10. Regex.run(~r/(\w+)@([\w.]+)/, email) |> validate_email
  11. rescue
  12. _ -> {:error, "Invalid email"}
  13. end
  14. end
  15. end
  16.  
  17. # check the email against a list of accepted domains, then make check if it is unique
  18. def validate_email([email, username, host]) do
  19. case host in Config.accepted_domains do
  20. true ->
  21. case find_by_email(email) do
  22. nil -> :ok
  23. _account -> :account
  24. end
  25. _ ->
  26. {:error, "Not an accepted domain."}
  27. end
  28. end
  29. end
Add Comment
Please, Sign In to add comment