How to validate password complexity

GreenValley
Wed, 30 Jul 2008 07:40:33 -0000

Hi,

I have a user model that saves password_salt and password_hash in DB,
there is no password attribute.  I would like to validate the password
complexity with regular expression, but because password is not an
attribute validation always fails.  How can I accomplish this
validation using the validation framework?

Thanks.

class User < ActiveRecord::Base
  validates_format_of :password,
      :with => /^.*(?=.{6,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/

  def password=(pass)
    salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp
    self.password_salt, self.password_hash       salt,
Digest::SHA256.hexdigest(pass + salt)
  end

  def password
  end
end

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---