Fetching contributors…
Cannot retrieve contributors at this time
93 lines (61 sloc) 2.79 KB

validates_email_format_of Gem and Rails Plugin

Validate e-mail addresses against RFC 2822 and RFC 3696.


Installing as a gem:

gem install validates_email_format_of

Installing as a Ruby on Rails 2 plugin:

./script/plugin install

Or in your Rails 3 Gemfile

gem 'validates_email_format_of', :git => 'git://'


class Person < ActiveRecord::Base
  validates_email_format_of :email

# Rails 3
class Person < ActiveRecord::Base
  validates :email, :email_format => {:message => 'is not looking good'}

As of version 1.4, it's possible to run e-mail validation tests (including MX checks) without using ActiveRecord or even touching a database. The validate_email_format method will return nil on a valid e-mail address or an array of error messages for invalid addresses.

results = ValidatesEmailFormatOf::validate_email_format(email, options)

if results.nil?
  # success!
  puts results.join(', ')


   String. A custom error message (default is: " does not appear to be a valid e-mail address")
   Symbol. Specifies when this validation is active (default is :save, other options :create, :update)
   Boolean. Allow nil values (default is false)
   Boolean. Allow blank values (default is false)
   Boolean. Check domain for a valid MX record (default is false)
   Specifies a method, proc or string to call to determine if the validation should occur 
   (e.g. :if => :allow_validation, or :if => { |user| user.signup_step > 2 }). The method, 
   proc or string should return or evaluate to a true or false value. 
   See :if option.
  Maximum number of characters allowed in the local part (default is 64)
  Maximum number of characters allowed in the domain part (default is 255)


To execute the unit tests run rake test.

The unit tests for this plugin use an in-memory sqlite3 database.

Tested in Ruby 1.8.7, 1.9.2, 1.9.3-rc1, JRuby and REE 1.8.7.



Written by Alex Dunae (, 2006-11.

Many thanks to the plugin's recent contributors:

Thanks to Francis Hwang ( at Diversion Media for creating the 1.1 update.

Thanks to Travis Sinnott for creating the 1.3 update.

Thanks to Denis Ahearn at Riverock Technologies ( for creating the 1.4 update.

Thanks to George Anderson ( and 'history' ( for creating the 1.4.1 update.