Sha256: 521e6a29fbe0db2b0ae3d15bbfdc49f017bba7a63f68eca80a1ed0ce2e329c57
Contents?: true
Size: 1.26 KB
Versions: 3
Compression:
Stored size: 1.26 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 symbol 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
3 entries across 3 versions & 1 rubygems