Rails 3: ActiveRecord Email Validation - validates_format_of

In Rails 3, the validates_format_of is deprecated. I sometimes get annoyed with all the deprecation that goes on with Rails (and Ruby, to a lesser extent), but this one is not a big deal, and the newer way is better, IMHO.

The old way:

  validates_presence_of :email
  validates_uniqueness_of :email
  validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create

The new way:

validates :email,   
            :presence => true,   
            :uniqueness => true,   
            :format => { :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i 
By Albert on March 6, 2011 3:02 PM

Categories:

2 Comments

At Rails3 example you've missed close paren. Anyway, works great. Thanks!

Thanks Chmarusso!