Class: PhoneNumberValidator::Validator
- Inherits:
-
Object
- Object
- PhoneNumberValidator::Validator
- Defined in:
- lib/phone_number_validator/validator.rb
Overview
This class does all of the matching and checking work for the validation of the phone number.
Constant Summary collapse
- PHONE_NUMBER_REGEX =
The regex pattern for validating US phone numbers
Regular Expression tested using regexr.com
The regular expression used can be found in README.rdoc on the documentation’s home page
Regexp.new('^(?:(?:[2-9]11)|(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:\x20+|#|x\.?|ext\.?|extension)\s*(\d+))?)$', Regexp::IGNORECASE)
Instance Method Summary collapse
-
#initialize(phone_number = '+1 (987) 654-3210 ext. 198842') ⇒ Validator
constructor
Initializes the @phone_number instance variable.
-
#validate ⇒ Object
Return Type: boolean or nil.
Constructor Details
#initialize(phone_number = '+1 (987) 654-3210 ext. 198842') ⇒ Validator
Initializes the @phone_number instance variable
15 16 17 |
# File 'lib/phone_number_validator/validator.rb', line 15 def initialize(phone_number = '+1 (987) 654-3210 ext. 198842') @phone_number = phone_number end |
Instance Method Details
#validate ⇒ Object
Return Type: boolean or nil
Examples
Ex. 1
require 'phone_number_validator'
check_phone_number = PhoneNumberValidator.validate_phone_number('+1 (987) 654-3210 ext. 198842')
print check_phone_number
Output
true
Ex. 2
require 'phone_number_validator'
check_phone_number = PhoneNumberValidator.validate_phone_number('+1 (987 778873-321a0 ext.ff99')
print check_phone_number
Output
false
Ex. 3
require 'phone_number_validator'
check_phone_number = PhoneNumberValidator.validate_phone_number('')
print check_phone_number
Output
nil
61 62 63 64 65 66 67 68 69 |
# File 'lib/phone_number_validator/validator.rb', line 61 def validate if (@phone_number.match(PHONE_NUMBER_REGEX)) return true elsif (@phone_number == '' || @phone_number == nil) return nil else return false end end |