Skip to content

Instantly share code, notes, and snippets.

@scudco
Created February 16, 2011 22:02
Show Gist options
  • Save scudco/830344 to your computer and use it in GitHub Desktop.
Save scudco/830344 to your computer and use it in GitHub Desktop.
Rails 3 Email Validator
class EmailValidator < ActiveModel::EachValidator
# http://fightingforalostcause.net/misc/2006/compare-email-regex.php
# Thanks to James Watts and Francisco Jose Martin Moreno
EMAIL_REGEX = /^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i
def validate_each(object,attribute,value)
if value.blank?
object.errors[attribute] << (options[:message] || "can't be blank") unless options[:allow_blank]
elsif !(value =~ EMAIL_REGEX)
object.errors[attribute] << (options[:message] || "is not a valid email address")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment