Phone validation
PhoneNumberValidation() checks whether a phone number is valid and returns it in national and international format, with its country and network.
package main
import (
"context"
"fmt"
"log"
idealpostcodes "go.ideal-postcodes.co.uk/sdk"
)
func main() {
client := idealpostcodes.NewClient("ak_test")
res, err := client.PhoneNumberValidation(context.Background(), "+44 20 7112 8019", nil)
if err != nil {
log.Fatal(err)
}
phone := res.Result.AsPhoneNumber()
if phone == nil {
fmt.Println("Invalid number")
return
}
fmt.Println(phone.InternationalFormat, phone.Country)
if c := phone.OriginalCarrier; c != nil && c.Name != nil {
fmt.Println(*c.Name)
}
}
Include the country code. A UK number can start +44, 44 or 0044. Your key needs phone validation enabled.
Result
A valid number returns a PhoneNumber with:
| Field | Example |
|---|---|
NationalFormat | 020 7112 8019 |
InternationalFormat | +44 20 7112 8019 |
ISOCountry and ISOCountry2 | GBR and GB |
Country | United Kingdom |
OriginalCarrier | A *Carrier for the network the number was first assigned to, with Name and NetworkType such as mobile or landline, each a *string |
An invalid number returns an InvalidPhoneNumber with Valid false and every other field nil. It is not an error, so AsPhoneNumber() returns nil and you check for that before reading the formats.
Current network
Set CurrentCarrier to "true" in PhoneNumberValidationParams to fill CurrentCarrier on the result as well. A number can move to another network, so OriginalCarrier 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 PhoneNumberValidation() for the parameters and the phone validation guide for how the checks work.