Sha256: ed222002be890e2bc25006b45e97d23244fc840c95414ee7877cacd45ec90a05
Contents?: true
Size: 1.31 KB
Versions: 7
Compression:
Stored size: 1.31 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 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 end
Version data entries
7 entries across 7 versions & 1 rubygems