Skip to Content
ZarexDocs

Phone validation

Check phone numbers, filter by carrier and country, with the official numbering plans of every country.

Phone validation checks the number typed into a form: format, line type, carrier and country. It covers mobile and landline numbers from every country, with their carriers.

The problem

In many countries, the phone number is the main identifier: mobile payments, SMS verification codes. An invalid number makes a payment or an SMS you pay for fail. A VoIP number makes it possible to create accounts in bulk. A premium-rate number can cost you a lot if you send it SMS. And numbering plans change: in Benin, numbers moved to 10 digits.

What it stops

  • invalid numbers or numbers in the old format;
  • VoIP numbers, often used for fake accounts;
  • premium-rate numbers, used for SMS fraud;
  • carriers or countries you do not serve: for example, only accepting MTN and Moov for a Mobile Money payment.

When to use it

Phone sign-up, SMS codes, Mobile Money payment, delivery.

Configuration

The number to check is passed with call:

server.ts
import express from "express";
import { zarex, phoneValidation } from "@zarex/express";

const app = express();
app.use(express.json());

app.post(
  "/payment",
  zarex({
    key: process.env.ZAREX_KEY!,
    rules: [
      phoneValidation({
        countryCode: "BJ",
        operators: ["MTN", "MOOV"],
        deny: ["INVALID", "VOIP"],
      }),
    ],
    call: (req) => ({ phone: req.body.phone }),
  }),
  (req, res) => res.json({ ok: true })
);

With no option, only invalid numbers (INVALID) are denied. Old 8-digit Beninese numbers are among them: they are denied with the more precise reason LEGACY_FORMAT, so you can ask the visitor for their number in the new 10-digit format.

Without a number, the rule checks nothing

If call provides no number (empty field or missing from the form), the rule lets the request through (skipped: "phone_absent"). Make the field required in your own validation: otherwise, a script only has to leave it out.

What can be denied

deny valueNumber denied
INVALIDA number that does not exist in its country's numbering plan (wrong length, unassigned prefix…).
LEGACY_FORMATOld 8-digit Beninese format. Also denied by INVALID.
VOIPVoIP number.
FIXEDLandline.
PREMIUM_RATEPremium-rate number.
TOLL_FREEToll-free number.

Two other denial reasons come from the countries and operators options: COUNTRY (country not accepted) and OPERATOR (carrier not accepted).

Options

OptionValuesDefaultRole
denysee above["INVALID"]What is denied.
countryCode2-letter ISO codenoneCountry of a number written without a country code (01 97 12 34 56).
countriesISO codes (64 at most), or "AFRICA"allAccepted countries. "AFRICA" covers the whole continent.
operatorscarrier names (64 at most)allOnly accept these carriers.
mode"LIVE" · "DRY_RUN" · "OFF""LIVE"Deny, only observe, or disable.

deny replaces the default list: write the full list, for example ["INVALID", "VOIP"].

countryCode: numbers written without a country code

A number written with its country code (+229 01 97 12 34 56) is always recognized. Without a country code, Zarex cannot guess the country: without countryCode, 0197123456 is denied as invalid. If your visitors type their local number, give their country:

phoneValidation({ countryCode: "BJ" }),

operators: for Mobile Money, for example

Names are those the decision shows in operator, ignoring case and spaces: MTN, MOOV, CELTIIS, ORANGE, AIRTEL, TOGOCOM, VODAFONE, SAFARICOM, GLO…

phoneValidation({ countryCode: "BJ", operators: ["MTN", "MOOV"] }),

The carrier is deduced from the number

The carrier comes from the number range originally assigned. A subscriber who switched carriers while keeping their number (number portability) may show up under the old one. A number whose carrier is unknown is never denied by operators. For a payment, your Mobile Money provider has the final say.

mode: observe before blocking

In DRY_RUN, nothing is blocked: the decision log shows the numbers that would have been denied. Useful before enabling operators or countries.

What the rule does not check

A valid number is a plausible number: the rule does not check that it can be reached, or that it belongs to the person typing it. For that, send an SMS code.

What the decision contains

The rule's result, in decision.results (ruleType: "PHONE_VALIDATION"), contains in reason:

FieldMeaning
is_validThe number exists in its country's numbering plan.
country_codeThe number's country ("BJ", "TG"…).
line_typeMOBILE, FIXED, FIXED_OR_MOBILE, VOIP, PREMIUM_RATE, TOLL_FREE…
operatorThe carrier, when known.
legacy_formattrue: old 8-digit Beninese format.
deniedThe reasons for the denial (INVALID, VOIP, COUNTRY, OPERATOR…).
skipped"phone_absent": no number provided, nothing was checked.

The number itself never appears in the decision or in the decision log.

Examples, with countryCode: "BJ". An MTN number:

{ "is_valid": true, "country_code": "BJ", "line_type": "MOBILE", "operator": "MTN" }

A Moov number, with operators: ["MTN"]:

{ "is_valid": true, "country_code": "BJ", "line_type": "MOBILE", "operator": "MOOV", "denied": ["OPERATOR"] }

An old 8-digit number:

{ "is_valid": false, "legacy_format": true, "denied": ["LEGACY_FORMAT"] }

On this page