npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

validate-contact-number

v0.0.4

Published

![Test suite](https://github.com/jankapunkt/npm-package-template/workflows/Test%20suite/badge.svg) [![Build Status](https://travis-ci.org/jankapunkt/npm-package-template.svg?branch=master)](https://travis-ci.org/jankapunkt/npm-package-template) [![JavaScr

Downloads

1

Readme

:package: validate-contact-number :package:

Test suite Build Status JavaScript Style Guide Project Status: Active – The project has reached a stable, usable state and is being actively developed. GitHub

About this package

A simpler and smaller NPM package which can be used to verify and format contact numbers. The package takes advantage of Regex patterns, rather than relying on Google’s libphonenumber library’s validation rules, to validate different types of contact numbers for different countries.

Installation

With NPM:

npm install validate-contact-number --save

With Yarn:

yarn add validate-contact-number

Usage

With Frontend frameworks like React, Angular, Vue, Svelte.

import { isValidContactNumber } from "validate-contact-number";

In Node:

const validateContactNumberPackage = require("validate-contact-number");

In React-Native:

import { isValidContactNumber } from "validate-contact-number";

API

import {
  isValidContactNumber,
  isValidLandlineNumber,
  isValidMobileNumber,
  validateContactNumber,
  formatContactNumber,
} from "validate-contact-number";

isValidContactNumber

Returns a boolean (true | false) based on whether a contact number (mobile | landline) belongs to a country. The functions accepts two argument contactNumber and countryISO | countryISO[]

Example:

// javascript
import { isValidContactNumber } from "validate-contact-number";
const validContactNumber = "9876543210"; // same goes for 09876543210, +919876543210
const countryISO = "IN"; // Indian ISO

console.log(isValidContactNumber(validContactNumber, countryISO)); // true
import { isValidContactNumber } from "validate-contact-number";
const validContactNumber = "2212345678"; // same goes for +91 22 1234 5678
const countryISOList = ["IN", "MY", "AF"]; // a list of countries

console.log(isValidContactNumber(validContactNumber, countryISOList)); // true

isValidMobileNumber

Returns a boolean (true | false) based on whether a mobile number belongs to a country. The functions accepts two argument mobileNumber and countryISO | countryISO[]

Example:

import { isValidMobileNumber } from "validate-contact-number";
const validMobileNumber = "9876543210"; // same goes for 09876543210, +919876543210
const countryISO = "IN"; // Indian ISO

console.log(isValidMobileNumber(validMobileNumber, countryISO)); // true
import { isValidMobileNumber } from "validate-contact-number";
const validMobileNumber = "9876543210"; // same goes for 09876543210, +919876543210
const countryISOList = ["IN", "MY", "AF"]; // a list of countries

console.log(isValidMobileNumber(validMobileNumber, countryISOList)); // true

isValidLandlineNumber

Returns a boolean (true | false) based on whether a landline number belongs to a country. The functions accepts two argument landlineNumber and countryISO | countryISO[]

Example:

import { isValidLandlineNumber } from "validate-contact-number";
const validLandlineNumber = "+91 22 1234 5678"; // same goes for 22 1234 5678
const countryISO = "IN"; // Indian ISO

console.log(isValidLandlineNumber(validLandlineNumber, countryISO)); // true
import { isValidLandlineNumber } from "validate-contact-number";
const validLandlineNumber = "+91 22 1234 5678"; // same goes for 22 1234 5678
const countryISOList = ["IN", "MY", "AF"]; // a list of countries

console.log(isValidLandlineNumber(validLandlineNumber, countryISOList)); // true

validateContactNumber

Returns a detailed response about contact number like country name, country code, contact type etc. Functions accepts a required contactNumber as argument. The contact number is matched with available regex patterns of countries. The country details are returned when contact number satisfies the country regex (either mobile or landline). Function also accepts optional argument countryISO | countryISO[] which can narrow down the available regex patterns against which the contact number needs to be checked.

Example:

// Without countryISO
import { validateContactNumber } from "validate-contact-number";
const validLandlineNumber = "+91 22 1234 5678"; // same goes for 9876543210

const contactDetails = validateContactNumber(validLandlineNumber);
// conatctDetails: {valid: true, countryISO: "IN", contactNumber: "2212345678", dialCode: "+91", countryName: "India", contactType: "landline" }

// invalid number
const invalidLandlineNumber = "+91 22 1234 56";
const contactDetails = validateContactNumber(invalidLandlineNumber);
// conatctDetails: {valid: false }
// With countryISO
import { validateContactNumber } from "validate-contact-number";
const validLandlineNumber = "+91 22 1234 5678"; // same goes for 9876543210
const countryISO = "IN"; // Indian ISO

const contactDetails = validateContactNumber(validLandlineNumber, countryISO);
// conatctDetails: {valid: true, countryISO: "IN", contactNumber: "2212345678", dialCode: "+91", countryName: "India", contactType: "landline" }

// invalid number
const invalidLandlineNumber = "+91 22 1234 56";
const countryISO = "IN"; // Indian ISO
const contactDetails = validateContactNumber(invalidLandlineNumber, countryISO);
// conatctDetails: {valid: false }
// With a list of countryISO
import { validateContactNumber } from "validate-contact-number";
const validLandlineNumber = "+91 22 1234 5678"; // same goes for 9876543210
const countryISOList = ["IN", "MY", "AF"]; // a list of countries

const contactDetails = validateContactNumber(
  validLandlineNumber,
  countryISOList
);
// conatctDetails: {valid: true, countryISO: "IN", contactNumber: "2212345678", dialCode: "+91", countryName: "India", contactType: "landline" }

// pattern not satisfied
const validLandlineNumber = "+91 22 1234 5678"; // same goes for 9876543210
const countryISOList = ["SG", "MY", "AF"]; // a list of countries
const contactDetails = validateContactNumber(validLandlineNumber, countryISO);
// conatctDetails: {valid: false }

formatContactNumber,

Returns a number in a specified format. Function accepts contactNumber and contactFormat as required arguments. The formats can be given in patterns of d. Examples:

Example:

import { formatContactNumber } from "validate-contact-number";
const contactNumber = "9876543210";

console.log(formatContactNumber(contactNumber, "+91dddddddddd")); // +919876543210
console.log(formatContactNumber(contactNumber, "+91-dd-dddd-dddd")); // +91-98-7654-3210
console.log(formatContactNumber(contactNumber, "dd dddd dddd")); // 98 7654 3210
console.log(formatContactNumber(contactNumber, "dd dddd-dddd dd.dd")); // 98 7654-3210