Class: EmailValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/validators/email_validator.rb

Overview

Validate email, also works with UTF-8/IDN.

The validation is intentionally simply, you can never be sure an email is valid anyway (a user can mistype gmail as gmaill, for example).

  • User part: one or more characters except @ or whitespace.

  • Domain part: 1 or more character except @ or whitespace.

  • tld part: 2 or more word characters, numbers, or -

Constant Summary collapse

REGEXP =
/\A
  [^@\s]+
  @
  ([\p{L}\d-]+\.)+  # \p{L} is any unicode character in the category "Letter"
  [\p{L}\d\-]{2,}
  \z
/ix.freeze

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



20
21
22
# File 'lib/validators/email_validator.rb', line 20

def validate_each record, attribute, value
  record.errors.add attribute, (options[:message] || I18n.t('rails_validations.email.invalid')) unless value =~ REGEXP
end