@jil/phonenumber
v0.2.0
Published
An utility for validating the phone number for security purposes
Downloads
14
Readme
@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'