nigeria-phone-number-validator
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

Nigerian Phone Number Validator

CircleCI JavaScript Style Guide NPM Total Download

A simple function that validates and returns common Nigerian Telco numbers.

Installation

npm install nigeria-phone-number-validator

OR

<!-- Place script tag in root (CDN) -->
<script defer src="https://bundle.run/nigeria-phone-number-validator@1.0.0"></script>

Usage

// Synchronous Usage
import { validatePhoneNumberSync } from "nigeria-phone-number-validator";

const result1 = validatePhoneNumberSync("08033445673");
console.log(result);
// { telco: "MTN", isValid: true }

const result2 = validatePhoneNumberSync("02033445673");
console.log(result2);
//   {
//     errors: [ "Phone number doesn't match a valid service provider" ],
//     isValid: false,
//   }
// Asynchronous Usage
import { validatePhoneNumberAsync } from "nigeria-phone-number-validator";

validatePhoneNumberAsync("08033445673").then((result) => {
	console.log(result);
	// { telco: "MTN", isValid: true }
});

validatePhoneNumberAsync("02033445673").catch((result) => {
	console.log(result);
	//   {
	//     errors: [ "Phone number doesn't match a valid service provider" ],
	//     isValid: false,
	//   }
});

try {
	const result = await validatePhoneNumberAsync("08033445673");
	console.log(result);
	// { telco: "MTN", isValid: true }
} catch (error) {
	// react to error
}

CDN USAGE

// The methods are attached to the windows object
window
.nigeriaPhoneNumberValidator
.validatePhoneNumberAsync("08033445673").then((result) => {
	console.log(result);
	// { telco: "MTN", isValid: true }
});

window
.nigeriaPhoneNumberValidator
.validatePhoneNumberAsync("02033445673").catch((result) => {
	console.log(result);
	//   {
	//     errors: [ "Phone number doesn't match a valid service provider" ],
	//     isValid: false,
	//   }
});

Package Sidebar

Install

npm i nigeria-phone-number-validator

Weekly Downloads

103

Version

1.0.4

License

ISC

Unpacked Size

3.29 kB

Total Files

2

Last publish

Collaborators

  • shaolinmkz