Skip to the content.

yup-phone MIT License npm - yup-phone Tweet yup-phone validator

Build Status Commitizen friendly FOSSA Status Known Vulnerabilities Codacy Badge

Adds a phone number validation check to yup validator using google-libphonenumber which gives accurate validation checks.
Read more about the core library here libphonenumber.
Read more about yup validator here yup

Install

# npm install --save yup-phone
$ npm add yup-phone

Test

Check validation in Codesandbox

Examples

// See https://repl.it/repls/WiryCleverPatches
import * as yup from 'yup';
// const yup = require("yup");
import "yup-phone";
// require("yup-phone");

// validate any phone number (defaults to India for country)
const phoneSchema = yup.string()
  .phone()
  .required();

(async () => {
  console.log(await phoneSchema.isValid("9876543210")); // → true
})();

// See https://repl.it/repls/SwiftImpossibleCertification
import * as yup from 'yup';
// const yup = require("yup");
import "yup-phone";
// require("yup-phone");

// validate phone number loosely in the given region
const phoneSchema = yup.string()
  .phone("IN")
  .required();

(async () => {
  console.log(await phoneSchema.isValid('+919876543210')); // → true
})();

// See https://repl.it/repls/PartialAlicebluePrediction
import * as yup from 'yup';
// const yup = require("yup");
import "yup-phone";
// require("yup-phone");

// validate phone number strictly in the given region
const phoneSchema = yup.string()
  .phone("IN", true)
  .required();

console.log(phoneSchema.isValidSync("+919876543210")); // → true

// See https://repl.it/repls/UniqueForsakenDownloads
import * as yup from 'yup';
// const yup = require("yup");
import "yup-phone";
// require("yup-phone");

// validate phone number strictly in the given region with custom error message
const phoneSchema = yup.string()
  .phone('IN', true, '${path} is invalid')
  .required();

try {
  phoneSchema.validateSync('+1 345 9490088');
} catch (error) {
  console.log(error.message); // → this is invalid
}

For more examples, check yup-phone.test.ts file.

Module Sizes

Destination: dist/yup-phone.umd.js
Bundle Size:  544.08 KB
Minified Size:  537.41 KB
Gzipped Size:  107.04 KB 
Destination: dist/yup-phone.umd.min.js
Bundle Size:  508.65 KB
Minified Size:  506.46 KB
Gzipped Size:  105.73 KB 
Destination: dist/yup-phone.esm.js
Bundle Size:  648 B
Minified Size:  646 B
Gzipped Size:  370 B 
Destination: dist/yup-phone.cjs.js
Bundle Size:  1.35 KB
Minified Size:  1.34 KB
Gzipped Size:  662 B 

Contributing

$ npm run build # Build for production
$ npm test # Run tests
$ npm publish # Publish npm package (prompts for version)

License

MIT.

FOSSA Status