Class: PhoneValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/simple_phone_validation/phone_validator.rb

Constant Summary collapse

PHONE_REG_EXP =
/((?:\+|00)[17](?: |\-)?|(?:\+|00)[1-9]\d{0,2}(?: |\-)?|(?:\+|00)1\-\d{3}(?: |\-)?)?(0\d|\([0-9]{3}\)|[1-9]{0,3})(?:((?: |\-)[0-9]{2}){4}|((?:[0-9]{2}){4})|((?: |\-)[0-9]{3}(?: |\-)[0-9]{4})|([0-9]{7}))/

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/simple_phone_validation/phone_validator.rb', line 5

def validate_each(record, attribute, value)
  return if options[:allow_blank] && value.blank?

  unless value =~ PHONE_REG_EXP
    record.errors[attribute] << (options[:message] || I18n.t('global.errors.phone_format'))
  end
end