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

Matches:

2342343423, 1-800-232-1232, 100-232-2322, 1905-232-2323

Does not match:

3434343434343, 333333333, 905-232-23239, (111) 232-2322

Examples:

validates :phone, phone_number: true

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/omg_validator/validators/phone_number_validator.rb', line 15

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