Pro unlocks unlimited templates48:00:00Get Pro →

Phone Input with Validation

Same country picker and flags as Phone Input, plus national formatting, per-country validation, and onValidationChange for form gating. Digits are kept when the country changes.

Live status

onValidationChange drives the hint below — try an invalid US number and blur to see the error border.

Loading…

Installation

CLI
npx @sonojs/ui@latest add phone-input-validation
Also needs
npx @sonojs/ui@latest add dialog input input-group phone-input popover table
npm i libphonenumber-js lucide-react

Usage

phone-input-validation.tsx
"use client"

import * as React from "react"
import { Button } from "@/components/ui/button"
import { Field, FieldError, FieldLabel } from "@/components/ui/field"
import {
  PhoneInputValidation,
  type PhoneValidationState,
} from "@/components/phone-input-validation"

export function Example() {
  const [state, setState] = React.useState<PhoneValidationState>("empty")
  const [attempted, setAttempted] = React.useState(false)

  return (
    <form
      onSubmit={(e) => {
        e.preventDefault()
        setAttempted(true)
        if (state !== "valid") return
        // send API…
      }}
    >
      <Field data-invalid={attempted && state !== "valid" ? true : undefined}>
        <FieldLabel htmlFor="phone">Phone</FieldLabel>
        <PhoneInputValidation
          id="phone"
          defaultCountry="US"
          onValidationChange={setState}
        />
        {attempted && state === "invalid" ? (
          <FieldError>Enter a valid phone number.</FieldError>
        ) : null}
      </Field>
      <Button type="submit" className="mt-3">
        Continue
      </Button>
    </form>
  )
}

Props

PropTypeDefaultDescription
defaultCountrystring"US"Initial ISO2 country code when uncontrolled.
countrystring-Controlled ISO2 country code.
onCountryChange(country: PhoneCountry) => void-Fires when the selected country changes.
valuestring-Controlled national number (formatted or digits; sanitized internally).
defaultValuestring""Initial national number when uncontrolled.
onChange(value: string) => void-Fires with the formatted national number.
onValueChange(payload) => void-Formatted national, raw digits, validation state, and optional E.164 when valid.
onValidationChange(state: "empty" · "incomplete" · "valid" · "invalid") => void-Fires whenever the validation state changes.
invalidboolean-Force invalid styling regardless of derived state.
showFlagbooleantrueWhen `false`, hides flag images and shows dial codes only.
flagCdnBasestringSonoJS flags CDNCDN base for flag SVGs (no trailing slash).
disabledbooleanfalseDisables the country picker and input.
classNamestring-Merge Tailwind classes on the input group root.