class PhoneNumberValidator::Validator

Constants

PHONE_NUMBER_REGEX

Validates US Phone Numbers

Regular Expression tested using regexr.com

Raw Regular Expression:

/^(?:(?:[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+))?)$/i

RegEx Flags:

Regexp::IGNORECASE (i)

Public Class Methods

new(phone_number = '+1 (949) 355-6244 ext. 198842') click to toggle source

Initialize Phone Number

Method type:

constructor
# File lib/phone_number_validator/validator.rb, line 20
    def initialize(phone_number = '+1 (949) 355-6244 ext. 198842')
@phone_number = phone_number
    end

Public Instance Methods

validate_phone_number() click to toggle source

Validation Method

Checks to see if the phone number the user entered is valid by testing regex the pattern with the phone number

Return Type:

boolean
# File lib/phone_number_validator/validator.rb, line 32
def validate_phone_number
  if (@phone_number.match(PHONE_NUMBER_REGEX))
    puts "'%s' is a valid phone number." % @phone_number
    return true
  else
    puts "'%s' is not a valid phone number." % @phone_number
    return false
  end
end