GooseUI

Components

Phone Input

New

A phone field with a country select and a mask that is applied as you type. The caret stays in place when you insert or delete in the middle, pasted numbers are cleaned up and extra digits are dropped. Outside you get an E.164 string and a validity flag. No phone library inside: every country of the world is built in. The list is generated from libphonenumber data and a monthly job refreshes it, so you never maintain it by hand. Country names come from Intl.DisplayNames, the data itself is about 3 kB gzipped. JavaScript is unavoidable here: it formats the number and keeps the caret in place. The country list is a native popover with search and keyboard control, flags are emoji, icons are inline SVG.

Browser Support

:has()

Highlights errors in nested fields

Your browser:
:focus-within

Highlights focused nested fields

Your browser:

Preview

We call only to confirm the order

Installation

npx shadcn@latest add @gooseui/phone-input
pnpm dlx shadcn@latest add @gooseui/phone-input
yarn dlx shadcn@latest add @gooseui/phone-input
bunx --bun shadcn@latest add @gooseui/phone-input

Usage

import { PhoneInput } from "@/components/ui/phone-input"

export default function Example() {
  const [phone, setPhone] = useState("")
  const [valid, setValid] = useState(false)

  return (
    <PhoneInput
      defaultCountry="GB"
      value={phone}
      onValueChange={(value, { isValid }) => {
        setPhone(value) // "+442079460958"
        setValid(isValid)
      }}
    />
  )
}

Examples

Paste and autofill

A number with a plus or 00 picks the country by its calling code. For +7 a leading 8 is treated as the domestic prefix, so 8 900… becomes +7 900…. Digits beyond the longest valid number of the country are dropped, never more than 15 in total as E.164 requires.

Copy a sample, then paste it into the field

8 (900) 123-45-67+7 701 234 5678+44 20 7946 09580049 30 1234 5678+1 415 555 0123 ext 99

Value sent to the form: nothing entered

<PhoneInput defaultCountry="RU" onValueChange={setPhone} />
// paste "8 (900) 123-45-67"   → "+79001234567"
// paste "+7 701 234 5678"     → "+77012345678", country KZ
// paste "0049 30 1234 5678"   → "+493012345678", country DE

Limited list of countries

By default the list holds every country, with RU, KZ, BY and UZ on top as a separate group. Pass countries to narrow it: filter PHONE_COUNTRIES or write your own entries (mask uses # for each digit, minLength and maxLength set the valid lengths). preferredCountries changes the top group, codes missing from the list are skipped. Search works on the whole list by name and by calling code: +49 or 49.

Value sent to the form: +31612345678, the number is complete

import { PHONE_COUNTRIES } from "@/lib/phone-countries"

const europe = PHONE_COUNTRIES.filter((c) =>
  ["NL", "DE", "FR", "PL", "ES", "IT"].includes(c.code),
)

<PhoneInput
  countries={europe}
  preferredCountries={["NL", "DE"]}
  defaultValue="+31612345678"
/>

Value sent to the form

The first argument of onValueChange is the number in E.164: a plus, the country code and digits with no spaces. The empty field gives an empty string. isValid says whether the number is complete.

Value sent to the form: nothing entered

<PhoneInput
  onValueChange={(value, { isValid }) => {
    // value: "+79161234567", isValid: true
  }}
/>

Validation in a form

An unfinished number turns into an error once focus leaves the field: the border goes red and the text appears under it. The browser also blocks the submit with the same message. With name set the E.164 value goes into a hidden input.

<form action={save}>
  <PhoneInput
    name="phone"
    required
    labels={{ incomplete: "Enter the full phone number" }}
  />
</form>

// External error instead of the built-in one
<PhoneInput error={serverError} />

Props

PropTypeDefaultDescription
valuestring-Controlled value in E.164
defaultValuestring-Initial value in E.164
onValueChange(value: string, meta: PhoneValueMeta) => void-Called with the E.164 value and { isValid, country }
defaultCountrystring"US"ISO code of the country on mount
onCountryChange(country: PhoneCountry) => void-Called when the country changes
countriesPhoneCountry[]all countriesCountries to offer instead of the full built-in list, for example a filtered PHONE_COUNTRIES
preferredCountriesstring[]["RU", "KZ", "BY", "UZ"]ISO codes shown on top as a separate group, in the given order
countryLabelstring"Country"Accessible label of the country button
labelsPartial<{ country, search, empty, incomplete, required }>-Texts: country, search, empty, incomplete (error for an unfinished number), required (error for an empty required field)
errorReactNode-External error, string or node. Replaces the built-in one and paints the field red
validatebooleantrueBuilt-in check: the error under the field after focus leaves an unfinished number. false turns it off
localestring-BCP 47 tag, country names are translated with Intl.DisplayNames
namestring-Name of the hidden input with the E.164 value
inputClassNamestring-Classes for the inner text input
classNamestring-Classes for the outer frame

PhoneCountry

PropTypeDefaultDescription
codestring-ISO 3166-1 alpha-2 code
namestring-Fallback name, the list takes names from Intl.DisplayNames
dialCodestring-Calling code without the plus
maskstring-Mask of the national number, # is a digit
minLengthnumber-Fewest digits for a valid number, defaults to the mask length
maxLengthnumber-Most digits for a valid number, defaults to the mask length
prefixesstring[]-National prefixes that tell apart countries sharing a code
trunkPrefixstring-Domestic prefix dropped on paste
flagstring-Emoji flag, built from code when omitted

Features

  • Stable caret: the position is counted in digits, so the mask never throws it to the end
  • Mask-aware delete: Backspace on a bracket or dash removes the digit before it
  • Country list: native popover with search, arrows, Enter and Esc, roles listbox and option
  • Mobile keyboard: inputMode="tel" and autoComplete="tel"