Home / Forms and Input / Phone Input with Country Codes

Phone Input with Country Codes in JavaScript, Free with Live Demo

Free international phone number input in plain JavaScript. Searchable country list with dial codes, formatting as you type, length checks and E.164 output for your server.

Open live demoDownload HTML fileView code on GitHub
Phone Input with Country Codes JavaScript project: a phone number field with a searchable country list and automatic formatting

Runs on: Intl.DisplayNames. Works in all modern browsers.

What is the Phone Input with Country Codes?

A phone field with a country picker. Choose a country from a searchable list or type a dial code, and the number is formatted the local way as you type.

The field tells you how many digits are missing, and shows the clean international version that you would send to your server or SMS service.

Good for

  • Sign up and checkout forms
  • Delivery and booking contact details
  • SMS verification flows
  • International lead forms

What this project does

How it works

  1. Country data in one listEach country has an ISO code, dial code, a display mask and the expected number of digits. Names come from Intl.DisplayNames in the visitor's language.
  2. Format as you typeOnly digits are kept, a leading 0 is dropped, and the mask adds spaces and brackets in the right places.
  3. Send one clean valueThe server gets the number in E.164 format: a plus sign, the dial code and the digits, with no spaces.

The key JavaScript

This is the heart of the project. The full file has the rest, including the screen layout and error handling.

const names = new Intl.DisplayNames(["en"], { type: "region" });
names.of("BD");                           // "Bangladesh"

function format(digits, mask) {           // mask like "####-######"
  let out = "", i = 0;
  for (const ch of mask) {
    if (i >= digits.length) break;
    out += ch === "#" ? digits[i++] : ch;
  }
  return out;
}
const e164 = "+" + country.dial + digits; // "+8801712345678"

How to use it

  1. Click Download HTML file above.
  2. Open the file in a code editor, like VS Code.
  3. Run it from a local server with npx serve . so the camera, microphone and AI features are allowed.
  4. Change the text and colors, then upload it to GitHub Pages, Netlify or your own site. It is one file with no build step.

Questions people ask

What is E.164?

The international phone format: a plus sign, country code and number with no spaces, like +447700900123. SMS services expect it.

Why no flag emoji?

Flag emoji do not show on Windows, so the demo uses short country codes that look the same everywhere.

Is a length check enough?

It catches most typos. For full checks, use a phone library on your server or verify the number with a code.

More Forms and Input projects