Sha256: 9f13e9ca89fe23cf5a7cab81d3570787438321c4a3ef76b31dd8ad43528ed728

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 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))
      puts "\"%s\" is a valid phone number." % @phone_number
      return true
    elsif (@phone_number == '')
      puts 'No phone number entered'
      return :nil
    else
      puts "\"%s\" is not a valid phone number." % @phone_number
      return false
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
phone_number_validator-0.5.3 lib/phone_number_validator/validator.rb
phone_number_validator-0.5.2 lib/phone_number_validator/validator.rb