Phone validation
phone_number_validation checks whether a phone number is valid and returns it in national and international format, with its country and network.
require "ideal_postcodes"
client = IdealPostcodes::Client.new(api_key: "ak_test")
phone = client.phone_number_validation(query: "+44 20 7112 8019").result
if phone.is_a?(IdealPostcodes::Types::PhoneNumber)
puts "#{phone.international_format} #{phone.country} #{phone.original_carrier&.name}"
end
Include the country code. A UK number can start +44, 44 or 0044. Your key needs phone validation enabled.
Results
A valid number returns a PhoneNumber with:
| Reader | Example |
|---|---|
national_format | 020 7112 8019 |
international_format | +44 20 7112 8019 |
iso_country and iso_country_2 | GBR and GB |
country | United Kingdom |
original_carrier | A Carrier for the network the number was first assigned to, with name and network_type such as mobile or landline |
An invalid number returns an InvalidPhoneNumber with valid false and every other reader nil. It is not an error, so check the class before reading the formats.
Current network
Add current_carrier: true alongside query: to fill current_carrier as well. A number can move to another network, so original_carrier alone can be out of date. The extra check can be slow, depending on the network, so allow for it in your request timeout.
Cost
One lookup per request, including numbers that are invalid or fail to parse.
See validate for the keywords and the phone validation guide for how the checks work.