-
-
Save churcho/a179dfc7be90c8dcefa2ce19494624ce to your computer and use it in GitHub Desktop.
ELIXIR - Validating a password for length and complexity in Ecto
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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