Giter Site home page Giter Site logo

fredygerman / tanzanian-phone-validator Goto Github PK

View Code? Open in Web Editor NEW
9.0 1.0 2.0 1018 KB

Phone Number Validaiton for Tanzanian.

Home Page: https://www.npmjs.com/package/tanzanian-phone-validator

License: MIT License

TypeScript 100.00%
hacktoberfest phone-number phone-validation phone-validator tanzania

tanzanian-phone-validator's Introduction

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.

Prefix Operator Trading as Operational[1]
61 Viettel Tanzania Limited halotel yes
62 Viettel Tanzania Limited halotel yes
63 Mkulima African Telecommunication Company Limited Amotel no
64 Wiafrica Tanzania Limited CooTel no
65 MIC Tanzania Limited tiGo yes
66 Smile Communications Tanzania Limited smile yes
67 MIC Tanzania Limited tiGo yes
68 Airtel Tanzania Limited airtel yes
69 Airtel Tanzania Limited airtel yes
71 MIC Tanzania Limited tiGo yes
72 MO Mobile Holding Limited no
73 Tanzania Telecommunications Company Ltd TTCL yes
74 Vodacom Tanzania Limited Vodacom yes
75 Vodacom Tanzania Limited Vodacom yes
76 Vodacom Tanzania Limited Vodacom yes
77 MIC Tanzania Limited tiGo yes
78 Airtel Tanzania Limited airtel yes
79 Benson Informatics Ltd Smart no

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

tanzanian-phone-validator's People

Contributors

fredygerman avatar mikenandi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

tanzanian-phone-validator's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.