Created
May 30, 2015 19:45
-
-
Save warmwaffles/bca81eb9115da9fac933 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'mail' | |
| class EmailValidator < ActiveModel::EachValidator | |
| def valid_email?(value) | |
| a = Mail::Address.new(value) | |
| if a.domain && a.address == value | |
| elements = a.domain.split(/\./) | |
| a.domain.match(/\A\S+\Z/) && elements.none?(&:blank?) && elements.size > 1 | |
| else | |
| false | |
| end | |
| rescue Mail::FieldParseError | |
| return false | |
| end | |
| def validate_each(record, attribute, value) | |
| unless valid_email?(value) | |
| record.errors[attribute] << (options[:message] || I18n.t('errors.messages.email.invalid')) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment