Created
August 21, 2012 21:38
-
-
Save jacobsimeon/3419644 to your computer and use it in GitHub Desktop.
Password Complexity
This file contains 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
# Regular expression to require the following | |
# Thanks to Scott Chamberlain on Stack Overflow | |
# http://stackoverflow.com/questions/3721843 | |
# All ASCII printing characters | |
# At least 8 characters in length | |
# At least 1 lowercase letter | |
# At least 1 uppercase letter | |
# At least 1 special character | |
# No white-space characters | |
# At least 1 number | |
requirements = /(?=^[!-~]{8,}$)(?=.*[a-z])(?=.*[A-Z])(?=.*[^A-Za-z0-9])(?=^.*[^\s].*$)(?=.*[\d]).*$/ | |
[ | |
"passwor", # not long enough | |
"P@SSWORD2", # no lowercase letter | |
"p@ssword2", # no capital letter | |
"Password2", # no special character | |
"P @ssword2", # whitespace | |
"P_ssword", # no number | |
"P_ssword2" # underscores as valid 'special character' | |
].each do |password| | |
message = password[requirements].nil? ? "not ok" : "ok" | |
puts "#{password} #{message}" | |
end | |
#=> passwor not ok | |
#=> P@SSWORD2 not ok | |
#=> p@ssword2 not ok | |
#=> Password2 not ok | |
#=> P @ssword2 not ok | |
#=> P_ssword not ok | |
#=> P_ssword2 ok |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment