Class: OmgValidator::Validators::PhoneNumberValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- OmgValidator::Validators::PhoneNumberValidator
- 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
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 |