Skip to content

shaolinmkz/nigeria-phone-number-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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,
	//   }
});