@muhammad-tanko/duniya-phonenumber-validator
v1.0.0
Published
Universal phone number validator
Downloads
3
Maintainers
Readme
Nigerian Phone Number Validator
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/[email protected]"></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,
// }
});