Phone validation
PhoneNumberValidation() checks a phone number from any country and returns it in national and international format, with its country and the network it was assigned to.
package main
import (
"context"
"fmt"
"log"
addresszen "go.addresszen.com/sdk"
)
func main() {
client := addresszen.NewClient("ak_test")
res, err := client.PhoneNumberValidation(context.Background(), "+1 202 555 0123", nil)
if err != nil {
log.Fatal(err)
}
phone := res.Result.AsPhoneNumber()
if phone == nil {
fmt.Println("Invalid phone number")
return
}
fmt.Println(phone.InternationalFormat, phone.ISOCountry)
if c := phone.OriginalCarrier; c != nil && c.Name != nil {
fmt.Println(*c.Name)
}
}The number must include its country code: +1 for the US and Canada, +81 for Japan. Your key needs phone validation enabled.
Result
A valid number returns a PhoneNumber with Valid true and:
| Field | Contents |
|---|---|
NationalFormat | The number as dialed inside its country |
InternationalFormat | The number with its country code |
ISOCountry and ISOCountry2 | Three- and two-letter country codes, such as USA and US |
Country | Country name |
OriginalCarrier | A *Carrier for the network the number was first assigned to, with Name and NetworkType such as mobile, landline or virtual, 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 on some networks, 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.