Skip to content

As-you-type input

import { formatCNICInput, checkCNICInput, formatPhoneInput, checkPhoneInput, formatIBANInput, checkIBANInput } from 'pakdatakit/input'

Format a field while the user types, and tell a half-typed value apart from a wrong one, so you don’t show an error before they’ve finished. Each formatter returns the value to show in the input; each checker returns an InputState: 'empty', 'incomplete', 'valid' or 'invalid'.

checkCNICInput() Incomplete: keep typing

formatCNICInput() shows
35202-12
InputState
'incomplete'

Mark the field invalid only for 'invalid'. An 'incomplete' value can still become valid, so it is not an error yet. In React, for example:

const [cnic, setCnic] = useState('');
const state = checkCNICInput(cnic);
<input
value={cnic}
onChange={(e) => setCnic(formatCNICInput(e.target.value))}
aria-invalid={state === 'invalid'}
/>;

For ready-made inputs in React, Vue, Angular and Flutter, and for schema validation, see Forms.

import { formatCNICInput, checkCNICInput, formatPhoneInput, checkIBANInput } from 'pakdatakit';
formatCNICInput('3520212345');
// → '35202-12345'
checkCNICInput('35202-12');
// → 'incomplete'
formatPhoneInput('+923001234567');
// → '+92 300 1234567'
checkIBANInput('GB29');
// → 'invalid'
type InputState = 'empty' | 'incomplete' | 'valid' | 'invalid';
  • empty: nothing entered yet
  • incomplete: what has been typed so far can still become valid
  • valid: complete and valid
  • invalid: cannot become valid by typing more (wrong character, prefix or length)
function formatCNICInput(value: string): string;

Parameters. value: the current input value.

Returns the value to show in the input, grouped as 35202-1234567-1 as the groups fill.

Caveats. Keeps up to 13 digits; Urdu digits are converted and other characters are dropped.

Related: checkCNICInput(), CNIC.

function checkCNICInput(value: string): InputState;

Parameters. value: the current input value, formatted or not.

Returns an InputState. A value that cannot start a CNIC, such as 05202 (no province starts with 0), is 'invalid' straight away.

Caveats. A complete value is 'valid' when validateCNIC() accepts it, so the same limits apply: it is a format check, not identity verification.

Related: formatCNICInput(), the CNIC checker.

function formatPhoneInput(value: string): string;

Parameters. value: the current input value.

Returns the value to show in the input: 0300-1234567, or +92 300 1234567 when the user starts with the country code.

Related: checkPhoneInput(), Phone.

function checkPhoneInput(value: string): InputState;

Parameters. value: the current input value, formatted or not.

Returns an InputState for a Pakistani mobile number.

Caveats. The 03XX prefix is checked as soon as it is complete, so an unallocated prefix such as 0360 is 'invalid' before the rest is typed.

Related: formatPhoneInput(), validatePhone().

function formatIBANInput(value: string): string;

Parameters. value: the current input value.

Returns the value to show in the input: uppercase, in groups of four (PK36 SCBL 0000 0011 2345 6702), up to 24 characters.

Related: checkIBANInput(), IBAN.

function checkIBANInput(value: string): InputState;

Parameters. value: the current input value, formatted or not.

Returns an InputState for a Pakistani IBAN. A value that does not start with PK, such as GB29, is 'invalid'.

Caveats. The PK prefix, check digits and bank code are checked as they are typed; the full checksum only once all 24 characters are in.

Related: formatIBANInput(), the IBAN checker.