Class: OmgValidator::Validators::StrongPasswordValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/omg_validator/validators/strong_password_validator.rb

Overview

Checks whether password input is a strong password Must contain a least one number, one lower case letter and one upper case letter and must be at least 8 characters

Matches:

ASdj3j3jsS, 8#adCje3, pwd#fdJa9, To34zNbsr30,

Does not match:

password, sdfsdfs3, Jsdsdsdj, G3hn$h

Examples:

validates :password, strong_password: true

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/omg_validator/validators/strong_password_validator.rb', line 15

def validate_each(record, attribute, value)
  return nil if value.blank?
  reg = /(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/
  unless reg.match(value)
    record.errors[attribute] = "must contain at least a number, a lower case letter, a upper case letter and must be at least 8 characters"
  end
end