BigW Consortium Gitlab

email_validator.rb 515 Bytes
Newer Older
1 2
# EmailValidator
#
3
# Based on https://github.com/balexand/email_validator
4
#
5 6 7 8 9 10
# 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
11
  PATTERN = /\A\s*([-a-z0-9+._']{1,64})@((?:[-a-z0-9]+\.)+[a-z]{2,})\s*\z/i.freeze
12 13

  def validate_each(record, attribute, value)
14
    unless value =~ PATTERN
15 16 17 18
      record.errors.add(attribute, options[:message] || :invalid)
    end
  end
end