Skip to content

Instantly share code, notes, and snippets.

@lucasdavila
Created January 16, 2013 03:16
Show Gist options
  • Save lucasdavila/4544367 to your computer and use it in GitHub Desktop.
Save lucasdavila/4544367 to your computer and use it in GitHub Desktop.
Password strength with regex in Ruby
# example of using lookahead assertion to test password strength
# test if a given string contains at least a lowercase letter, a uppercase, a digit, a special char and 8+ chars
strong = "123ABCabc-"
strong[/^(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[\W]).{8,}$/]
# test if a given string contains at least a lowercase letter, a uppercase, a digit and 8+ chars
medium = "123ABCabc"
medium[/^(?=.*[a-zA-Z])(?=.*[0-9]).{8,}$/]
@rn-modi
Copy link

rn-modi commented Oct 29, 2023

Use this instead:

/\A(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*\W).{8,}\z/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment