Sha256: ade6c7bf6105e1e403e4988fb1ca53e9b56210fd85247dd582af74ebc2700de6

Contents?: true

Size: 1.24 KB

Versions: 4

Compression:

Stored size: 1.24 KB

Contents

##
# This class does all of the matching and checking work for the validation of the phone number.
class PhoneNumberValidator::Validator

	##
  # The regex pattern for validating US phone numbers
  #
  # Regular Expression tested using {regexr.com}[https://regexr.com/]
  #
  # The regular expression used can be found in <em>README.rdoc</em> on the documentation's home page
  PHONE_NUMBER_REGEX = 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)

  ##
  # initializes the <b>@phone_number</b> instance variable
	def initialize(phone_number = '+1 (949) 355-6244 ext. 198842')
    @phone_number = phone_number
	end

	##
	# Checks to see if the phone number the user entered is valid
	# by testing regex the pattern with the phone number
  #
  # <b>Return Type:</b>
  #   boolean or symbol
  def validate
    if (@phone_number.match(PHONE_NUMBER_REGEX))
      return true
    elsif (@phone_number == '')
      return :nil
    else
      return false
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
phone_number_validator-0.5.7 lib/phone_number_validator/validator.rb
phone_number_validator-0.5.6 lib/phone_number_validator/validator.rb
phone_number_validator-0.5.5 lib/phone_number_validator/validator.rb
phone_number_validator-0.5.4 lib/phone_number_validator/validator.rb