Phone input

A phone number with its country: picked from a list you can search by name, code or dial code, formatted as it is typed, and posted as E.164 (+447911123456).

Phone input

Needs Alpine and the package's script for the country picker, the formatting and E.164. Without them it is a native select of dial codes and a tel field, posting name[country] and name[number]. See getting started.

This is not libphonenumber. It knows each country's dial code, one common way of writing its numbers and roughly how long they are. It will not tell a real number from a made-up one. Validate on the server, for example with propaganistas/laravel-phone.

Use it

<x-ds::phone-input name="phone" label="Phone" :value="$user->phone" />

<x-ds::phone-input
    name="phone"
    label="Mobile"
    country="IN"
    :preferred="['IN', 'US', 'GB']"
    countryName="phone_country"
    :flags="true"
/>

{{-- Only these, no search field (six or fewer) --}}
<x-ds::phone-input name="phone" label="Phone" :countries="['GB', 'IE', 'FR', 'DE']" />

<x-ds::phone-input name="phone" label="Phone" wire:model.blur="phone" />

What it posts

With the scriptWithout it
name+447911123456 (E.164), or ""—
name[country]—GB
name[number]—07911 123456, as typed
countryNameGB, when you set it—

Accept both shapes in your request, or validate the no-script one by joining the two. wire:model and x-model bind the E.164 string.

Props

PropTypeDefaultWhat it does
namestring—The field the number posts under.
labelstring—The field's label.
valuestring—E.164. A number without "+" is read as national, in country.
countrystringthe app locale's region, else USISO code of the country shown when value does not say.
countriesarrayevery countryISO codes to offer. Only these are sent to the page.
preferredarray[]ISO codes pinned to the top, in this order. The rest follow by name.
countryNamestring—With the script, the chosen country's ISO code posts under this name too.
flagsboolfalseShow each country's flag emoji. Off by default: Windows draws them as two letters.
placeholderstring—The number field's placeholder.
countryLabelstring"Country"The picker's name.
searchPlaceholderstring"Search countries"The search field, shown for more than six countries.
emptyTextstring"No countries match"Said when the search matches nothing.
helpstring—Guidance under the field.
errorstring—Replaces help, marks the field invalid.
sizemd | smmdsm is 32px, for a table row.
disabledboolfalse

Anything else (id, required) goes on the number field; class on the root. The country data is resources/data/countries.php; an unknown code throws.

What it does

  • Typing formats the number for its country (98765 43210 for India, (415) 555-0123 for the US) and keeps the caret where it was.
  • A leading 0, the trunk prefix most countries dial at home, stays on screen and is dropped from E.164: 07911 123456 in the UK posts +447911123456. Italy and a few others keep theirs, as E.164 does.
  • A number starting + or 00, typed or pasted, chooses its country: paste +1 415 555 0123 and the picker moves to the United States. Where several countries share a code, the field's own country wins if it is one of them, then the main one (+1 is the US, +44 the UK).
  • Leaving the field checks the length only. A number clearly too short or too long for the country is marked invalid with a note. The form still submits: the server decides.
  • The picker searches as you type: "germ" finds Germany, "gb" the United Kingdom first, "+44" every country on +44. Choosing one keeps the number and closes the list.

PHP has the same reading, for a value you hold:

use Shipbytes\BladeUi\Support\Phone;

Phone::parse('+447911123456', ['GB', 'US'], 'US'); // ['country' => 'GB', 'national' => '7911123456']
Phone::e164('GB', '07911 123456');                 // "+447911123456"

Accessibility

  • The number is type="tel" with inputmode="tel" and autocomplete="tel-national"; the no-script select is autocomplete="tel-country-code", so autofill fills both.
  • The picker is a button that says the country ("Country: India, IN +91") and opens a popover with a listbox; arrows, Home, End, Enter and Escape work as in every list here, and focus comes back to the button.
  • The length note is announced politely and is part of the field's description. Flags are hidden from screen readers.

Don't

  • Don't treat a number it accepts as valid. See the note at the top.
  • Don't rely on flags alone to tell countries apart. The code and name are always shown.
  • Don't offer every country when you serve three. Pass countries; the page gets smaller and the list shorter.

More in specs/phone-input.md.


From the package repo: edit this page on GitHub.

Was this page helpful?