Class: PhoneValidator

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

Overview

Basic check for a telephone number; this should work with most, of not all, writing conventions.

Constant Summary collapse

REGEXP =
/
  \A
  (\(?\+\d+\)?)?      # optional country code
  [\d \-.()]{4,20}
  \z
/x.freeze

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



11
12
13
# File 'lib/validators/phone_validator.rb', line 11

def validate_each record, attribute, value
  record.errors.add attribute, (options[:message] || I18n.t('rails_validations.phone.invalid')) unless value =~ REGEXP
end