24 Oct 2006

Captcha in Ruby on Rails – Customize the use of captcha in the plugin validates_captcha

captcha, image, rails, ror, validations posted by Sur Max

Hello Everyone !!
I have released a captcha plugin Simple Captcha. It is really simple to implement, and provides a cool feature of multiple styles of images.



Previous Post for validates_captcha


To implement captcha in RubyonRails, validates_captcha plugin can be a good option but a small customization i need with this plugin was to use it on some specific action and not to be validated the captcha field every time an instance of the model is saved or updated.
Here is a small work-around for its customization…
How to use customized captcha in RoR ?
Install the plugin validates_captcha in your rails application by running this command from the root of your application

ruby script/plugin install http://svn.2750flesk.com/validates_captcha

Make sure that you can now see the directory vedor/plugins/validates_captcha.

Now run these commands from your application root to make the image and data directories

  ruby script/generate captcha store_directory
  ruby script/generate captcha image_directory

Here is the complete API for the usage of this plugin. I am describing the same idea as given in this API but in a bit more specific means.

Lets consider a model User in which we will implement the captcha.
Add the following code in the file app/models/user.rb

  class User < ActiveRecord::Base

    validates_captcha :if => :request_captcha_validation?
    attr_accessor :request_captcha_validation

    def request_captcha_validation?
      (self.request_captcha_validation==true)? true : false
    end

  end

Handle View and Controller

Add the code in the view inside your existing form.

  <% c = prepare_captcha :type => :image -%>
  <%= captcha_hidden_field c, 'user' %>
  <%= captcha_image_tag c %>
  <%= captcha_label 'user', 'Type in the text from the image above' %>
  <%= captcha_text_field 'user' %>

Your controller will look like

  def save
    # the line in bold represents that you need captcha validation.
    # if captcha validation is not required then remove this line from your controller.
    @user = User.new(params[:user])
    @user.request_captcha_validation = true
    @user.save
  end

However image is too noisy and it contains repeated strings.
To improve the quality of images generated by the plugin validates_captcha visit Here.

19 Oct 2006

Using Regular Expression in Ruby on Rails — Regexp for Password Validation

password, rails, regexp, validations posted by Sur Max

A regular expression (abbreviated as regexp or regex, with plural forms regexps, regexes, or regexen) is a string that describes or matches a set of strings, according to certain syntax rules. Regular expressions are used by many text editors and utilities to search and manipulate bodies of text based on certain patterns. Many programming languages support regular expressions for string manipulation. Ruby has a strong Regular Expression engine built directly as a class of Ruby Programming language as Regexp
Here we will go through an example which will validate the password string.
Lets say we have to implement the following validations to validate a password…

  • Password should contain atleast one integer.
  • Password should contain atleast one alphabet(either in downcase or upcase).
  • Password can have special characters from 20 to 7E ascii values.
  • Password should be minimum of 8 and maximum of 40 cahracters long.

To fulfill above requirements we can have a regular expression like…

/^(?=.*\d)(?=.*([a-z]|[A-Z]))([\x20-\x7E]){8,40}$/

in ruby programming language we can have a number of ways to define this regular expression as…
■ reg = Regexp.new(”^(?=.*\d)(?=.*([a-z]|[A-Z]))([\x20-\x7E]){8,40}$”)
or
■ reg = %r(^(?=.*\d)(?=.*([a-z]|[A-Z]))([\x20-\x7E]){8,40}$)
or simply
■ reg = /^(?=.*\d)(?=.*([a-z]|[A-Z]))([\x20-\x7E]){8,40}$/

Now look what exactly this regex is doing…
(?=.*\d) shows that the string should contain atleast one integer.
(?=.*([a-z]|[A-Z])) shows that the string should contain atleast one alphabet either from downcase or upcase.
([\x20-\x7E]) shows that string can have special characters of ascii values 20 to 7E.
{8,40} shows that string should be minimum of 8 to maximum of 40 cahracters long.
We can simply use this regular expression for manual handling of password in an action as…

def validate_password(password)

  reg = /^(?=.*\d)(?=.*([a-z]|[A-Z]))([\x20-\x7E]){8,40}$/

return (reg.match(password))? true : false

end

How to implement this regular expression in a model class in ruby on rails for password validation ?

To implement this regular expression in the model class in the rails way we can do it like…

class MyModel

  validates_format_of :password, :with => /^(?=.*\d)(?=.*([a-z]|[A-Z]))([\x20-\x7E]){8,40}$/

end