Validators
svelte-tel-input ships a separate subpath export for form validation — zero impact on your bundle if you don’t use it.
import { validateTelInput } from 'svelte-tel-input/validators';validateTelInput()
Section titled “validateTelInput()”validateTelInput( value: string, options?: TelValidatorOptions): ValidationErrorReturns a ValidationError (null when valid). Works with any schema library.
Options
Section titled “Options”interface TelValidatorOptions { required?: boolean; // default: false allowedCountries?: CountryCode[]; country?: CountryCode | null; // hint for numbers without '+'}Framework examples
Section titled “Framework examples”import { z } from 'zod';import { validateTelInput } from 'svelte-tel-input/validators';
const schema = z.object({ phone: z.string().superRefine((val, ctx) => { const error = validateTelInput(val, { required: true }); if (error) { ctx.addIssue({ code: 'custom', message: error }); } })});
schema.parse({ phone: '+36301234567' }); // ✓schema.parse({ phone: '' }); // ✗ 'REQUIRED'schema.parse({ phone: '+1999' }); // ✗ 'TOO_SHORT'import * as v from 'valibot';import { validateTelInput } from 'svelte-tel-input/validators';
const schema = v.object({ phone: v.pipe( v.string(), v.check( (val) => !validateTelInput(val, { required: true }), 'Invalid phone number' ) )});import * as yup from 'yup';import { validateTelInput } from 'svelte-tel-input/validators';
const schema = yup.object({ phone: yup.string().test( 'tel', 'Invalid phone number', (val) => !validateTelInput(val ?? '', { required: true }) )});Allowed countries
Section titled “Allowed countries”Restrict to a subset of countries:
const error = validateTelInput('+442012345678', { allowedCountries: ['US', 'HU']});// 'COUNTRY_NOT_ALLOWED' — GB is not in the listCountry hint
Section titled “Country hint”When validating numbers without a + prefix, pass a country hint:
validateTelInput('301234567', { country: 'HU' }); // null (valid)validateTelInput('301234567'); // 'INVALID' (no country context)Return values
Section titled “Return values”validateTelInput returns one of the ValidationError values:
| Return | Meaning |
|---|---|
null | Valid |
'REQUIRED' | Empty value with required: true |
'TOO_SHORT' | Not enough digits |
'TOO_LONG' | Too many digits |
'NOT_A_NUMBER' | Doesn’t look like a phone number |
'INVALID_COUNTRY' | Unrecognized country code |
'INVALID_LENGTH' | Length doesn’t match any valid pattern |
'COUNTRY_NOT_ALLOWED' | Valid number but country not in allowedCountries |
'INVALID' | Fallback |