2015-12-01 19:21:45 -05:00
|
|
|
# EmailValidator
|
|
|
|
#
|
2014-06-11 07:04:30 -04:00
|
|
|
# Based on https://github.com/balexand/email_validator
|
2015-02-03 00:31:03 -05:00
|
|
|
#
|
2014-06-11 07:04:30 -04:00
|
|
|
# Extended to use only strict mode with following allowed characters:
|
|
|
|
# ' - apostrophe
|
|
|
|
#
|
|
|
|
# See http://www.remote.org/jochen/mail/info/chars.html
|
|
|
|
#
|
|
|
|
class EmailValidator < ActiveModel::EachValidator
|
2015-12-01 19:21:45 -05:00
|
|
|
PATTERN = /\A\s*([-a-z0-9+._']{1,64})@((?:[-a-z0-9]+\.)+[a-z]{2,})\s*\z/i.freeze
|
2014-06-11 07:04:30 -04:00
|
|
|
|
|
|
|
def validate_each(record, attribute, value)
|
2015-12-01 19:21:45 -05:00
|
|
|
unless value =~ PATTERN
|
2014-06-11 07:04:30 -04:00
|
|
|
record.errors.add(attribute, options[:message] || :invalid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|