Unable to perform password complexity implementation in Ruby on Rails?
<p>I have researched a lot on this topic but due to some reason I am unable to perform password complexity implementation on my Ruby on Rails Web Application. I have installed the devise gem and followed <a href="https://stackoverflow.com/questions/22052473/best-flexible-rails-password-security-implementation">Best flexible rails password security implementation</a> and <a href="https://stackoverflow.com/questions/14430648/how-to-validate-password-strength-with-devise-in-ruby-on-rails">How to validate password strength with Devise in Ruby on Rails?</a>.</p>

<p>My regex seems to be working when I check it online </p>

<pre><code>/\A(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[[:^alnum:]])/x
</code></pre>

<p>but once I implement it inside my user.rb it just does not work. </p>

<p>My user.rb file:</p>

<pre><code>#Active Record for Users
class User &lt; ActiveRecord::Base
  belongs_to :entity
  has_and_belongs_to_many :groups, :join_table =&gt; "users_groups"
  has_many :surveys, inverse_of: :user
  has_many :results, inverse_of: :user

  validates :password, :firstName, :email, :salt, :role, :timezone, presence: true
  validates :email, :uniqueness =&gt; {:scope =&gt; :entity_id}
  validates_format_of :email, :with =&gt; /.+@.+\..+/i

  devise :database_authenticatable, :validatable
  validate :password_complexity

  #User Authentication
  def self.authenticate(email="", lpassword="")
    users = User.where(email: email)
    results = []
    users.each do |user|
      if user &amp;&amp; user.match_password(lpassword)
      results.push(user)
      end
    end
    if(results.length == 0)
    return false
    else
    return results
    end
  end

  #Password Check
  def match_password(lpassword="")
        return (BCrypt::Password.new(password).is_password?(lpassword+salt))
    end

  #Password Authentication
  def password_complexity
      if password.present? and not password.match(/\A(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[[:^alnum:]])/x)
        errors.add :password, "must include at least one lowercase letter, one uppercase letter, and one digit"
      end
   end

end
</code></pre>