Skip to content

Instantly share code, notes, and snippets.

@niaeashes
Created July 30, 2015 22:58
Show Gist options
  • Save niaeashes/81359397e496cd63dbf3 to your computer and use it in GitHub Desktop.
Save niaeashes/81359397e496cd63dbf3 to your computer and use it in GitHub Desktop.
class EmailValidator < ActiveModel::EachValidator
# Email address regex is copied from http://emailregex.com on "Ruby (simpler version)"
def validate_each(record, attr_name, value)
if ! ( value.blank? && options[:allow_blank] )
record.errors.add(attr_name, :invalid_email, options) unless value =~ /\A([\w+\-][\w+\-.]*[\w+\-]|[\w+\-.])@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment