Skip to content

Instantly share code, notes, and snippets.

@churcho
Forked from mike-north/user.ex
Created April 16, 2019 09:12
Show Gist options
  • Save churcho/a179dfc7be90c8dcefa2ce19494624ce to your computer and use it in GitHub Desktop.
Save churcho/a179dfc7be90c8dcefa2ce19494624ce 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