Class: OmgValidator::Validators::PhoneNumberValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/omg_validator/validators/phone_number_validator.rb

Overview

Checks whether input is a valid phone number Currently only supports American and Canadian formats

validates :phone, phone_number: true

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/omg_validator/validators/phone_number_validator.rb', line 8

def validate_each(record, attribute, value)
  return nil if value.nil?
  reg = /^(1?(-?\d{3})-?)?(\d{3})(-?\d{4})$/
  unless reg.match(value)
    record.errors[attribute] = "must be a valid phone number"
  end
end