1.1.5 • Published 7 months ago

tanzanian-phone-validator v1.1.5

Weekly downloads
-
License
ISC
Repository
github
Last release
7 months ago

Tanzanian Phone Number Validation

A simple npm package for validating Tanzanian phone numbers using regular expressions.

Installation

You can install this package via npm:

npm i tanzanian-phone-validator

Example

You can check an example of this library Here.

Here is how it look like:

image

Usage

Use the isValidPhoneNumber function to validate a phone number. It returns true if the phone number is valid and false otherwise.

Use the getPhoneNumberDetails function to get information about a phone number. It returns an object various information about the phone number as shown below:

const results = {
  isValid: true,
  telecomCompanyDetails: {
    prefix: 76,
    company: "Vodacom Tanzania Limited",
    brand: "Vodacom",
    operational: "yes",
  },
};

Usage with validation libraries

Here are some usage examples with different validation libraries:

Using Yup

const yup = require("yup");
import { isValidPhoneNumber } from "tanzanian-phone-validator";

const schema = yup.object({
  phoneNumber: yup
    .string()
    .test("is-tanzanian", "Invalid Tanzanian phone number", (value) => {
      return isValidPhoneNumber(value);
    }),
});

// Example usage
const data = {
  phoneNumber: "+255761234567",
};

schema
  .validate(data)
  .then((validatedData) => {
    console.log("Validation succeeded:", validatedData);
  })
  .catch((error) => {
    console.error("Validation failed:", error.message);
  });

Using Zod

const { z } = require("zod");
import { validateTanzanianPhoneNumber } from "tanzanian-phone-validator";

const schema = z.object({
  phoneNumber: z.string().refine(
    (value) => {
      return isValidPhoneNumber(value);
    },
    {
      message: "Invalid Tanzanian phone number",
    }
  ),
});

// Example usage
const data = {
  phoneNumber: "+255761234567",
};

try {
  const validatedData = schema.parse(data);
  console.log("Validation succeeded:", validatedData);
} catch (error) {
  console.error("Validation failed:", error.message);
}

Direct usage (Node Js)

You can also directly use the isValidPhoneNumber function:

import { validateTanzanianPhoneNumber } from "tanzanian-phone-validator";

const phoneNumber = "+255761234567";

if (isValidPhoneNumber(phoneNumber)) {
  console.log("Valid Tanzanian phone number.");
} else {
  console.log("Invalid Tanzanian phone number.");
}

List of Tanzanian mobile network operators and their prefixes

This table was used to used to create this package. IT can be found in wikipedia (Click here).. Note : this package does not validate for Operators which are not in Operation nor on this table.

PrefixOperatorTrading asOperational1
61Viettel Tanzania Limitedhalotelyes
62Viettel Tanzania Limitedhalotelyes
63Mkulima African Telecommunication Company LimitedAmotelno
64Wiafrica Tanzania LimitedCooTelno
65MIC Tanzania LimitedtiGoyes
66Smile Communications Tanzania Limitedsmileyes
67MIC Tanzania LimitedtiGoyes
68Airtel Tanzania Limitedairtelyes
69Airtel Tanzania Limitedairtelyes
71MIC Tanzania LimitedtiGoyes
72MO Mobile Holding Limitedno
73Tanzania Telecommunications Company LtdTTCLyes
74Vodacom Tanzania LimitedVodacomyes
75Vodacom Tanzania LimitedVodacomyes
76Vodacom Tanzania LimitedVodacomyes
77MIC Tanzania LimitedtiGoyes
78Airtel Tanzania Limitedairtelyes
79Benson Informatics LtdSmartno

License This package is licensed under the MIT License. See the LICENSE..

1.0.2

7 months ago

1.1.0

7 months ago

1.0.9

7 months ago

1.0.8

7 months ago

1.0.7

7 months ago

1.1.5

7 months ago

1.0.6

7 months ago

1.1.4

7 months ago

1.0.5

7 months ago

1.1.3

7 months ago

1.0.4

7 months ago

1.1.2

7 months ago

1.0.1

8 months ago

1.0.0

8 months ago