Skip to content

Instantly share code, notes, and snippets.

@jacobsimeon
Created August 21, 2012 21:38
Show Gist options
  • Save jacobsimeon/3419644 to your computer and use it in GitHub Desktop.
Save jacobsimeon/3419644 to your computer and use it in GitHub Desktop.
Password Complexity
# 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