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: 2 or more character except @ or whitespace.

  • tld part: 2 or more word characters or -

Constant Summary collapse

REGEXP =
/\A
  [^@\s]+
  @
  [^@\s.]+
  \.
  [\p{L}0-9\-]{2,}
\z/ix.freeze

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



18
19
20
# File 'lib/validators/email_validator.rb', line 18

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