# Phone input

SvPhoneInput on its own: a country dial-code selector plus a national number field emitting an E.164-ish string, with label / validation / dir.

Uses `@svgrid/grid` (MIT). Category: Inputs. Live demo: https://svgrid.com/demos/303-phone-input/

Install: `npm i @svgrid/grid` (Svelte 5).

## About this example

SvPhoneInput on its own in Svelte 5: a country dial-code selector with flags plus a national number field, emitting an E.164-style string such as +14155551234, with the shared field contract for label, validation and dir for RTL. It is the SvGrid phone cell editor, standalone.

SvPhoneInput on its own - a country dial-code selector plus a national number field, emitting an E.164-ish string. Carries the shared field contract (label / validation / dir). The SvGrid phone cell editor, standalone.

## Imports, features and API used

- Imports: `@svgrid/grid`

## Frequently asked questions

### What format is the value?

A single string with the plus sign, the dial code and the national digits, ready to store or send to an SMS provider. onChange also receives a parts object with country, dial, national, valid and complete, and the national part is masked per country for display.

### How is the country chosen?

From a dropdown listing flag, name and dial code; the national field then applies that country's mask, and the parts object tells you whether the number is complete and valid.

### Does it work right-to-left?

Yes. dir='rtl' mirrors the layout while the number itself stays left-to-right, as phone numbers should.

## Related documentation

- [SvPhoneInput](https://svgrid.com/docs/help/ui-components/sv-phone-input.md)

## Related articles

- [SvGrid UI Components Tips and Tricks: Svelte 5 Buttons, Inputs, Tree, Forms](https://svgrid.com/blog/svgrid-ui-components-tips-and-tricks/) - Practical tips for the SvGrid UI component kit - SvButton states, a Cmd+K command palette, a virtualized searchable tree, schema-driven forms, a parsing date/time field, and the shared field contract - each with a code snippet.

## Source code (303-phone-input.svelte)

```svelte
<script lang="ts">
  /**
   * SvPhoneInput on its own - a country dial-code selector plus a national number
   * field, emitting an E.164-ish string. Carries the shared field contract
   * (label / validation / dir). The SvGrid phone cell editor, standalone.
   */
  import { SvPhoneInput } from '@svgrid/grid'

  let a = $state('')
  let b = $state('')
  let work = $state('')
  // Per-country length validation from the phone parts (valid/complete).
  let workValid = $state(true)
  const workError = $derived(work && !workValid ? 'Not a valid number for the selected country' : undefined)
</script>

<div class="wrap">
  <header>
    <h2>Phone input</h2>
    <p><code>SvPhoneInput</code> - pick a country (flag + dial code), type the national number; emits <code>+&lt;dial&gt;&lt;digits&gt;</code>.</p>
  </header>

  <div class="col">
    <label class="cell">Default (US)
      <SvPhoneInput value={a} onChange={(v) => (a = v)} />
      <span class="val">{a || '-'}</span>
    </label>

    <label class="cell">Default country GB
      <SvPhoneInput value={b} country="GB" onChange={(v) => (b = v)} />
      <span class="val">{b || '-'}</span>
    </label>

    <div class="cell">
      <SvPhoneInput
        value={work}
        label="Work phone"
        hint="Validated against the selected country"
        required
        invalid={!!workError}
        error={workError}
        onChange={(v, parts) => { work = v; workValid = parts.valid }}
      />
    </div>
  </div>
</div>

<style>
  .wrap { padding: 20px; max-width: 560px; display: flex; flex-direction: column; gap: 18px; }
  header h2 { margin: 0 0 4px; font-size: 20px; font-weight: 700; }
  header p { margin: 0; color: var(--sg-muted, #64748b); font-size: 13.5px; line-height: 1.5; }
  code { background: var(--sg-row-hover-bg, #eef2ff); padding: 1px 5px; border-radius: 5px; font-size: 12px; }
  .col { display: flex; flex-direction: column; gap: 18px; align-items: flex-start; }
  .cell { display: flex; flex-direction: column; gap: 6px; font-size: 12px; font-weight: 600; color: var(--sg-muted, #64748b); }
  .val { font-size: 12px; color: var(--sg-muted, #94a3b8); font-weight: 400; font-variant-numeric: tabular-nums; }
</style>
```

[View this example on GitHub](https://github.com/sv-grid/sv-grid/blob/main/examples/src/demos/303-phone-input.svelte)
