Class: EmailFormatValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/email_format_validator.rb

Overview

Validate fields to look like emails

Is not an exhaustive check but prevents emails that can cause Postfix to crash. Specifically emails with special characters in the local part.

Does not consider a blank string as invalid.

Examples

validates :email, email_format: true                 # optional
validates :email, email_format: true, presence: true # required

Constant Summary collapse

EMAIL_REGEX =
/^(([^(),:;<>@\[\\\]\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}))?$/i

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attr_name, value) ⇒ Object



14
15
16
17
# File 'app/validators/email_format_validator.rb', line 14

def validate_each(record, attr_name, value)
  return if value.blank?
  record.errors.add(attr_name, :email_format, options) unless value =~ EMAIL_REGEX
end