Class: EmailValidator

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

Overview

Validate email, works with UTF-8 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 alphabet character or number or _ or -.

  • Domain part: one or more alphabet character or number or _ or -.

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

Constant Summary collapse

REGEXP =
/^([a-zA-Z0-9_-])+@(([a-zA-Z0-9_-])+)((\.([a-zA-Z0-9_-])+)*)((\.[a-zA-Z0-9_-]{2,3}){1,2})$/

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



10
11
12
# File 'lib/validations/email_validator.rb', line 10

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