@jil/phonenumber
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

@jil/phonenumber

A utility for validating the phone number for security purposes

Introduction

Validate and format a phone number.

Uses google-libphonenumber for validation.

Which is a compiled version of the Google library libphonenumber.

Usage

import {phoneValidator} from '@jil/phonenumber';

console.log(phoneValidator.validate('18511223344', {defaultCountry: 'CN'}));
// => '18511223344'
console.log(phoneValidator.validate('18511223344', {defaultCountry: 'CN', format: 'e164'}));
// => '+8618511223344'
console.log(phoneValidator.validate('18511223344', {defaultCountry: 'CN', format: 'international'}));
// => '+86 185 1122 3344'
console.log(phoneValidator.validate('18511223344', {defaultCountry: 'CN', format: 'national'}));
// => '+86 185 1122 3344'
console.log(phoneValidator.validate('18511223344', {defaultCountry: 'CN', format: 'rfc3966'}));
// => '185 1122 3344'
console.log(phoneValidator.validate('494322456', {defaultCountry: 'CN'}));
// => validation error: PhoneNumberValidationError

or create a new validator with multiple default countries:

import {PhoneNumberValidator} from '@jil/phonenumber';

const validator = PhoneNumberValidator.create({defaultCountry: ['CN', 'US']});

console.log(validator.validate('18511223344'));
// => '18511223344'
console.log(validator.validate('18511223344', {format: 'e164'}));
// => '+8618511223344'

Readme

Keywords

none

Package Sidebar

Install

npm i @jil/phonenumber

Weekly Downloads

16

Version

0.2.0

License

MIT

Unpacked Size

11.3 kB

Total Files

11

Last publish

Collaborators

  • towyuan