Sha256: 3410e73169b6faa9dbcb689e8c1169cece91b4e9e9257eef84b60abade7a1a45
Contents?: true
Size: 1.91 KB
Versions: 5
Compression:
Stored size: 1.91 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 (987) 654-3210 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 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 def validate if (@phone_number.match(PHONE_NUMBER_REGEX)) return true elsif (@phone_number == '' || @phone_number == nil) return nil else return false end end end
Version data entries
5 entries across 5 versions & 1 rubygems