I've built a User model and sign up along the lines of Michael Hartl's tutorial. I have adapted this regex for minimum password complexity to check for
- at least one lower case letter
- at least one upper case letter
- at least one number
- at least one symbol
i.e.
^(?=.*[A-Z])(?=.*[!@#$&*])(?=.*[0-9])(?=.*[a-z]).{8,}$
The regex works perfectly as intended in rubular, however, I have not been able to successfully implement it in my app
What I've tried so far
I've tried various combinations of
VALID_PASSWORD_REGEX = ^(?=.*[A-Z])(?=.*[!@#$&*])(?=.*[0-9])(?=.*[a-z]).{8,}$
validates :password, format: { with: VALID_PASSWORD_REGEX }
With and without:
- double quotations around the regex e.g. "^(?=.* ... {8,}$"
- forward slashes around /VALID_PASSWORD_REGEX/
- the # in the [!@#$&*] part of the regex ('#' seems to be recognised as a comment in sublime text)
I keep getting errors along the lines of "A regular expression or a proc or lambda must be supplied as :with" and "syntax error, unexpected '^'"
What worked
After reading assefamaru's answer, I was able to see the ^ and $ needed to be replaced with \A and \z (see here)
Here's what works:
VALID_PASSWORD_REGEX = /\A(?=.*[A-Z])(?=.*[!@$&*])(?=.*[0-9])(?=.*[a-z]).{8,}$\z/
validates :password, format: { with: VALID_PASSWORD_REGEX }