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
# 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: 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
- 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.