Adds a phone number validation check to yup validator using libphonenumber-js which gives accurate validation checks. Read more about yup validator here yup
# npm install --save yup-phone
$ npm add yup-phone
Check validation in Codesandbox
// 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.
Destination: dist/yup-phone.umd.js
Bundle Size: 220.83 KB
Minified Size: 114.36 KB
Gzipped Size: 27.88 KB
Destination: dist/yup-phone.umd.min.js
Bundle Size: 113.55 KB
Minified Size: 112.38 KB
Gzipped Size: 28 KB
Destination: dist/yup-phone.esm.js
Bundle Size: 481 B
Minified Size: 483 B
Gzipped Size: 299 B
Destination: dist/yup-phone.cjs.js
Bundle Size: 912 B
Minified Size: 914 B
Gzipped Size: 496 B
- Uses Rollup for bundling.
- Uses npm for package management.
- Files are minified using closure compiler.
- Uses jest for testing.
- Generates CJS, UMD, and ESM builds.
- Use
npm version [major|minor|patch]
to version. - Use tslint and prettier for code formatting.
- Uses semantic release for version.
- Use
npx cz
to create a standard commit interactively.
$ npm run build # Build for production
$ npm test # Run tests
$ npm publish # Publish npm package (prompts for version)
MIT.