class PortugueseValidators::PortuguesePhoneValidator
Validates Portuguese Phone numbers.
The portuguese phone numbers can start by 00351 or +351 and are composed by 9 digits. The number must start with one of the values in VALID_PREFIXES
Constants
- COUNTRY_PREFIXES
- VALID_PREFIXES
Public Instance Methods
is_valid?(number)
click to toggle source
# File lib/portuguese_validators/phone.rb, line 20 def is_valid?(number) looks_like_phone_number?(number.to_s) && valid_phone_number?(number.to_s) end
validate_each(record, attribute, value)
click to toggle source
# File lib/portuguese_validators/phone.rb, line 13 def validate_each(record, attribute, value) return if value.blank? phone_number = value.to_s.gsub(" ", "") record.errors.add(attribute, options[:message] || :invalid) unless is_valid?(phone_number) end
Private Instance Methods
looks_like_phone_number?(number)
click to toggle source
# File lib/portuguese_validators/phone.rb, line 32 def looks_like_phone_number?(number) number.match(/^((00|\+)(\d{3}))?\d{9}$/) ? true : false end
valid_phone_number?(number)
click to toggle source
# File lib/portuguese_validators/phone.rb, line 26 def valid_phone_number?(number) COUNTRY_PREFIXES.each { |pref| number.gsub!(pref, '') if number.start_with?(pref) } VALID_PREFIXES.each { |pref| return true if number.start_with?(pref) } false end