Sha256: 3e31270fa044ed58abf2704881c6a8e800ead8dae4b32f1cbfd805d3ccefab97

Contents?: true

Size: 630 Bytes

Versions: 4

Compression:

Stored size: 630 Bytes

Contents

module OmgValidator
  module Validators
    # Checks whether input is a valid phone number
    # Currently only supports American and Canadian formats
    #
    # validates :phone, phone_number: true
    class PhoneNumberValidator < ActiveModel::EachValidator
      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
    end
  end
end

ActiveModel::Validations::PhoneNumberValidator = OmgValidator::Validators::PhoneNumberValidator

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
omg_validator-0.0.6.2 lib/omg_validator/validators/phone_number_validator.rb
omg_validator-0.0.6.1 lib/omg_validator/validators/phone_number_validator.rb
omg_validator-0.0.6 lib/omg_validator/validators/phone_number_validator.rb
omg_validator-0.0.5 lib/omg_validator/validators/phone_number_validator.rb