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
Highlights errors in nested fields
Highlights focused nested fields
Preview
We call only to confirm the order
Installation
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
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 DELimited 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
| Prop | Type | Default | Description |
|---|---|---|---|
| value | string | - | Controlled value in E.164 |
| defaultValue | string | - | Initial value in E.164 |
| onValueChange | (value: string, meta: PhoneValueMeta) => void | - | Called with the E.164 value and { isValid, country } |
| defaultCountry | string | "US" | ISO code of the country on mount |
| onCountryChange | (country: PhoneCountry) => void | - | Called when the country changes |
| countries | PhoneCountry[] | all countries | Countries to offer instead of the full built-in list, for example a filtered PHONE_COUNTRIES |
| preferredCountries | string[] | ["RU", "KZ", "BY", "UZ"] | ISO codes shown on top as a separate group, in the given order |
| countryLabel | string | "Country" | Accessible label of the country button |
| labels | Partial<{ country, search, empty, incomplete, required }> | - | Texts: country, search, empty, incomplete (error for an unfinished number), required (error for an empty required field) |
| error | ReactNode | - | External error, string or node. Replaces the built-in one and paints the field red |
| validate | boolean | true | Built-in check: the error under the field after focus leaves an unfinished number. false turns it off |
| locale | string | - | BCP 47 tag, country names are translated with Intl.DisplayNames |
| name | string | - | Name of the hidden input with the E.164 value |
| inputClassName | string | - | Classes for the inner text input |
| className | string | - | Classes for the outer frame |
PhoneCountry
| Prop | Type | Default | Description |
|---|---|---|---|
| code | string | - | ISO 3166-1 alpha-2 code |
| name | string | - | Fallback name, the list takes names from Intl.DisplayNames |
| dialCode | string | - | Calling code without the plus |
| mask | string | - | Mask of the national number, # is a digit |
| minLength | number | - | Fewest digits for a valid number, defaults to the mask length |
| maxLength | number | - | Most digits for a valid number, defaults to the mask length |
| prefixes | string[] | - | National prefixes that tell apart countries sharing a code |
| trunkPrefix | string | - | Domestic prefix dropped on paste |
| flag | string | - | 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"