class PhoneNumberValidator::Validator
Constants
- PHONE_NUMBER_REGEX
Validates US Phone Numbers¶ ↑
Regular Expression tested using regexr.com
Raw Regular Expression:
/^(?:(?:[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+))?)$/i
RegEx Flags:
Regexp::IGNORECASE (i)
Public Class Methods
Public Instance Methods
validate_phone_number()
click to toggle source
Validation Method¶ ↑
Checks to see if the phone number the user entered is valid by testing regex the pattern with the phone number
Return Type:
boolean
# File lib/phone_number_validator/validator.rb, line 32 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