Sha256: aecb10b281d45306aedfe2b6a19858931adff507f5fa2fba37af7084b4de0dbe

Contents?: true

Size: 1.26 KB

Versions: 48

Compression:

Stored size: 1.26 KB

Contents

module Incline
  ##
  # Validates a string to ensure it contains a valid email address.
  #
  #   validates :email_address, 'incline/email' => true
  #
  class EmailValidator < ActiveModel::EachValidator

    INTERNAL_DOM_REGEX = '[a-z\d]+(?:-+[a-z\d]+)*(?:\.[a-z\d]+(?:-+[a-z\d]+)*)*\.[a-z]+'
    private_constant :INTERNAL_DOM_REGEX

    ##
    # This regular expression should validate 99.9% of common email addresses.
    #
    # There are some weird rules that it doesn't account for, but they should be rare.
    #
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@#{INTERNAL_DOM_REGEX}\z/i

    ##
    # This regular expression should validate any domain.
    VALID_DOMAIN_REGEX = /\A#{INTERNAL_DOM_REGEX}\z/i


    ##
    # Validates attributes to determine if they contain valid email addresses.
    #
    # Does not perform an in depth check, but does verify that the format is valid.
    def validate_each(record, attribute, value)
      unless value.blank?
        record.errors[attribute] << (options[:message] || 'is not a valid email address') unless value =~ VALID_EMAIL_REGEX
      end
    end

    ##
    # Validates that an email address is valid based on format.
    def self.valid?(email)
      return false if email.blank?
      !!(email =~ VALID_EMAIL_REGEX)
    end

  end
end


Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
incline-0.3.14 lib/incline/validators/email_validator.rb
incline-0.3.13 lib/incline/validators/email_validator.rb
incline-0.3.12 lib/incline/validators/email_validator.rb
incline-0.3.11 lib/incline/validators/email_validator.rb
incline-0.3.10 lib/incline/validators/email_validator.rb
incline-0.3.9 lib/incline/validators/email_validator.rb
incline-0.3.8 lib/incline/validators/email_validator.rb
incline-0.3.7 lib/incline/validators/email_validator.rb
incline-0.3.6 lib/incline/validators/email_validator.rb
incline-0.3.5 lib/incline/validators/email_validator.rb
incline-0.3.4 lib/incline/validators/email_validator.rb
incline-0.3.3 lib/incline/validators/email_validator.rb
incline-0.3.2 lib/incline/validators/email_validator.rb
incline-0.3.1 lib/incline/validators/email_validator.rb
incline-0.3.0 lib/incline/validators/email_validator.rb
incline-0.2.36 lib/incline/validators/email_validator.rb
incline-0.2.35 lib/incline/validators/email_validator.rb
incline-0.2.34 lib/incline/validators/email_validator.rb
incline-0.2.28 lib/incline/validators/email_validator.rb
incline-0.2.27 lib/incline/validators/email_validator.rb