Docunext


Rails 3 ActiveRecord Email Validation validates format of

March 6th, 2011

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

¥

Yearly Indexes: 2003 2004 2006 2007 2008 2009 2010 2011 2012 2013 2015 2019 2020 2022