Skip to content

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(
value: string,
options?: TelValidatorOptions
): ValidationError

Returns a ValidationError (null when valid). Works with any schema library.

interface TelValidatorOptions {
required?: boolean; // default: false
allowedCountries?: CountryCode[];
country?: CountryCode | null; // hint for numbers without '+'
}
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'

Restrict to a subset of countries:

const error = validateTelInput('+442012345678', {
allowedCountries: ['US', 'HU']
});
// 'COUNTRY_NOT_ALLOWED' — GB is not in the list

When validating numbers without a + prefix, pass a country hint:

validateTelInput('301234567', { country: 'HU' }); // null (valid)
validateTelInput('301234567'); // 'INVALID' (no country context)

validateTelInput returns one of the ValidationError values:

ReturnMeaning
nullValid
'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