Skip to content

CNIC

import { validateCNIC, getCNICInfo } from 'pakdatakit/cnic'

Checks the structure of a 13-digit Computerised National Identity Card number, and reads the two facts the number itself encodes: the province where it was first registered and the holder’s gender.

validateCNIC() Valid

Formatted
35202-1234567-1
Province
Punjab
Gender
Male
import { validateCNIC, getCNICInfo } from 'pakdatakit';
validateCNIC('35202-1234567-1');
// → true
validateCNIC('05202-1234567-1');
// → false
getCNICInfo('35202-1234567-1');
// → {
// "digits": "3520212345671",
// "formatted": "35202-1234567-1",
// "province": "Punjab",
// "gender": "male"
// }
function validateCNIC(cnic: string): boolean;

Parameters. cnic: 13 digits, plain or grouped 5-7-1 with hyphens, en dashes, spaces or dots. Urdu digits are accepted.

Returns true when the number has the right shape and starts with a province code from 1 to 8; otherwise false. It never throws.

Caveats. NADRA does not publish a check-digit algorithm, so the last digit is not verified. Separators are only accepted between the 5-7-1 groups.

Related: getCNICInfo(), the CNIC checker.

function getCNICInfo(cnic: string): CNICInfo | null;
interface CNICInfo {
digits: string; // 13 digits
formatted: string; // 35202-1234567-1
province: string; // where the CNIC was first registered
gender: 'male' | 'female';
}

Parameters. cnic: as for validateCNIC().

Returns the decoded parts, or null when the CNIC is not valid.

Caveats. The province is where the CNIC was first registered, not necessarily where the holder lives now. Gender comes from the last digit: odd for male, even for female.

Related: validateCNIC(), formatCNICInput() for as-you-type fields.