sur says ajax :on => rails

Thursday, October 19, 2006

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

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

Tuesday, October 17, 2006

How to improve the image quality and generate random string image in the plugin validates_captcha

Validates captcha is a good pluging to implement captcha in your rails application.
However i found that there is repetition of the string of the image and the quality of image is not that good. To get a good quality image and random string replace the code of the file /vendor/plugns/validates_captcha/lib/captcha_challenge.rb with the following code...

Visit Here for the latest modified source code.

Monday, October 16, 2006

Rake task to remove the image and audio files created by captcha

To add a rake task to the rails application which removes all the image and audio files created by captcha, create a file named remove_captcha_files.rake in the lib/tasks directory.
Add the following code to the lib/tasks/remove_captcha_files.rake file…


desc "Remove captcha images and audio files"
task :remove_captcha_files do
image_path = "#{RAILS_ROOT}/public/images/captcha/"
audio_path = "#{RAILS_ROOT}/public/captcha_audio/"
Dir.foreach(image_path){|file| File.delete(image_path+file) if (/^.*\.jpg$/).match(file)} if File.exist?(image_path)
Dir.foreach(audio_path){|file| File.delete(audio_path+file) if (/^.*\.wav$/).match(file)} if File.exist?(audio_path)
puts "Captcha files removed."
end


You can confirm that task is added to your app by running


To remove all the files created by captcha, simply run the command

from the command line from your application root.