Skip to content

Instantly share code, notes, and snippets.

@mike-north
Created May 28, 2016 04:58
Show Gist options
  • Save mike-north/97aaf3b1c0edbd036ec979b37214da34 to your computer and use it in GitHub Desktop.
Save mike-north/97aaf3b1c0edbd036ec979b37214da34 to your computer and use it in GitHub Desktop.
ELIXIR - Validating a password for length and complexity in Ecto
defmodule MyApp.User do
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
|> validate_format(:email, ~r/@/)
|> validate_length(:password, min: 8)
|> validate_format(:password, ~r/[0-9]+/, message: "Password must contain a number") # has a number
|> validate_format(:password, ~r/[A-Z]+/, message: "Password must contain an upper-case letter") # has an upper case letter
|> validate_format(:password, ~r/[a-z]+/, message: "Password must contain a lower-case letter") # has a lower case letter
|> validate_format(:password, ~r/[#\!\?&@\$%^&*\(\)]+/, message: "Password must contain a symbol") # Has a symbol
|> validate_confirmation(:password)
# and so on
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment