Class: EmailValidator

Inherits:
LocalizedEachValidator
  • Object
show all
Defined in:
lib/email_validation.rb

Overview

Validates email addresses. Uses the `invalid_email` error message key.

Options


| | | |:————-|:——————————————————————————————————————————————————————————————————————————————————————–| | `:format` | By default, uses a simple email regex that matches most email addresses. If `:rfc_5322`, uses a full RFC 5322 email regex that will match common typos as well. If `:rfc_5322_simple`, uses a simplified version of the previous regex to ignore some common typos. | | `:message` | A custom message to use if the email is invalid. | | `:allow_nil` | If true, `nil` values are allowed. |

Examples:

validates :email_address, email: true

Constant Summary collapse

RFC5322_REGEX =

Regular expression describing valid emails.

/(?:[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/i
RFC5322_SIMPLE_REGEX =
/[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i
BASIC_REGEX =
/[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}/i

Instance Method Summary collapse

Instance Method Details

#valid?(_, _, value) ⇒ Boolean


26
27
28
29
30
31
32
33
# File 'lib/email_validation.rb', line 26

def valid?(_, _, value)
  regex = case options[:format]
            when :rfc_5322 then RFC5322_REGEX
            when :rfc_5322_simple then RFC5322_SIMPLE_REGEX
            else BASIC_REGEX
          end
  value =~ /\A#{regex}\z/i || value =~ /\A.+\s+<#{regex}>\z/i
end