Re: How to validate password complexity

Christopher Kintner
Wed, 30 Jul 2008 20:52:14 -0000

On Wed, Jul 30, 2008 at 2:40 AM, GreenValley
<ahmet.dogramaci-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
>
> 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?

You can always override validate or validate_on_create

See: http://api.rubyonrails.com/classes/ActiveRecord/Validations.html


> 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
-~----------~----~----~----~------~----~------~--~---