npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

phone-validation-with-flag

v1.0.1

Published

A lightweight phone number input with country flag selector and dial code. Zero dependencies. Works with Vanilla JS, React, and Vue 3.

Readme

phone-validation-with-flag 📞

npm version license bundle size

A lightweight, zero-dependency international phone number input with country flag selector and dial code.
Works everywhere — Vanilla JS, React, Vue, or any framework.

  • Zero dependencies — no jQuery, no libphonenumber, nothing
  • 🌍 253 countries & territories with dial codes
  • 🏳 Real flag images — via flagcdn.com (works on all browsers including Windows)
  • 🔍 Searchable — search by country name, code, or dial code (+92, pak...)
  • Built-in validation — per-country digit length validation with red/green feedback
  • 🌙 Dark mode — auto-detects system preference
  • 📐 TypeScript — full type definitions included
  • 📦 ESM + CJS + UMD — works everywhere

Installation

npm install phone-validation-with-flag
# or
yarn add phone-validation-with-flag
# or
pnpm add phone-validation-with-flag

Quick Start

Vanilla JS

import { PhoneInput } from 'phone-validation-with-flag';

const phone = new PhoneInput('#my-phone', {
  defaultCountry: 'US',
  onChange: (value) => {
    console.log(value.full);      // '+13001234567'
    console.log(value.isValid);   // true / false
  },
});

React

import { PhoneInputComponent } from 'phone-validation-with-flag/react';

function MyForm() {
  const [phone, setPhone] = useState(null);

  return (
    <PhoneInputComponent
      defaultCountry="PK"
      theme="auto"
      onChange={setPhone}
    />
  );
}
// phone = { countryCode, dialCode, number, full, isValid }

Vue 3

<script setup>
import { ref } from 'vue';
import PhoneInput from 'phone-validation-with-flag/vue';
const phone = ref(null);
</script>

<template>
  <PhoneInput v-model="phone" default-country="PK" theme="auto" />
</template>

CDN (no build step)

<script src="https://unpkg.com/phone-validation-with-flag/dist/index.umd.js"></script>
<div id="my-phone"></div>
<script>
  const { PhoneInput } = window.PhoneValidationWithFlag;
  new PhoneInput('#my-phone', {
    defaultCountry: 'US',
    onChange: (val) => console.log(val.full),
  });
</script>

onChange Value

Every time the user interacts, onChange fires with:

{
  countryCode: 'PK',           // ISO 3166-1 alpha-2
  countryName: 'Pakistan',
  dialCode: '+92',
  number: '3001234567',        // local number typed by user
  full: '+923001234567',       // ready to store / submit
  isValid: true,               // false if too short / too long
}

Validation

Built-in digit-length validation per country — no extra dependencies needed.

| Behavior | Result | |----------|--------| | Empty field | No indicator | | Too short | 🔴 Red border + "Too short — need 10 digits" | | Too long | 🔴 Red border + "Too long — max 10 digits" | | Correct length | 🟢 Green border, isValid: true |


Form Validation — Disable Submit Button

The most common use case: keep the submit button disabled until the user enters a valid number for their country.

Vanilla JS

const submitBtn = document.getElementById('submit-btn');

new PhoneInput('#my-phone', {
  defaultCountry: 'US',
  onChange: (val) => {
    submitBtn.disabled = !val.isValid;
  },
});
<button id="submit-btn" disabled>Submit</button>

React

const [phone, setPhone] = useState(null);

<PhoneInputComponent onChange={setPhone} />
<button disabled={!phone?.isValid}>Submit</button>

Vue 3

<PhoneInput v-model="phone" />
<button :disabled="!phone?.isValid">Submit</button>

isValid is false when the field is empty or the digit count doesn't match the selected country's rules.


Options

| Option | Type | Default | Description | |--------|------|---------|-------------| | defaultCountry | string | 'US' | ISO code of the pre-selected country | | value | { countryCode, number } | null | Initial value | | placeholder | string | 'Phone number' | Input placeholder text | | searchPlaceholder | string | 'Search country...' | Dropdown search placeholder | | theme | 'auto'\|'light'\|'dark' | 'auto' | Color theme | | disabled | boolean | false | Disable the component | | flagType | 'image'\|'none' | 'image' | How to render flags | | flagsUrl | string | flagcdn.com | URL template for flags ({code} replaced) | | onChange | function | null | Fired on country change or number input | | onInput | function | null | Fired only on number input |


Public API

const phone = new PhoneInput('#el', options);

phone.getValue();                    // → { countryCode, dialCode, number, full, isValid }
phone.setValue('GB', '7911123456'); // set country + number programmatically
phone.setCountry('PK');             // change country only
phone.setNumber('3001234567');      // change number only
phone.reset();                      // clear to default
phone.enable();
phone.disable();
phone.destroy();                    // cleanup DOM + events

TypeScript

Full TypeScript types included — no @types/ package needed.

import { PhoneInput, PhoneInputOptions, PhoneValue } from 'phone-validation-with-flag';

const options: PhoneInputOptions = {
  defaultCountry: 'PK',
  onChange: (val: PhoneValue) => console.log(val.full),
};

const phone = new PhoneInput('#el', options);

License

MIT © GitCodeCruiser