Skip to content

Instantly share code, notes, and snippets.

@benphelps
Created May 28, 2012 12:31
Show Gist options
  • Save benphelps/2818925 to your computer and use it in GitHub Desktop.
Save benphelps/2818925 to your computer and use it in GitHub Desktop.
class EmailValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
record.errors[attribute] << (options[:message] || "is not an email")
end
end
end
class Person < ActiveRecord::Base
validates :email, :presence => true, :email => true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment