Class: ActiveModelValidators::EmailValidator

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

Overview

Email addresses validator

Constant Summary collapse

EMAIL_ADDRESS_REGEX =

Regular expression used for validation

EMAIL_ADDRESS_REGEX = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attr, value) ⇒ Object

  • record - ActiveRecord model

  • attr - model attribute where email address will be stored

  • value - entered email address

Example

class Model < ActiveRecord::Base
  validates :my_email, :'active_model_validators/email' => true
end


18
19
20
21
22
# File 'lib/active_model_validators/email_validator.rb', line 18

def validate_each(record, attr, value)
  unless value =~ EMAIL_ADDRESS_REGEX
    record.errors.add(attr, :invalid)
  end
end