# CNIC

> Validate Pakistani CNIC numbers and read the province of registration and gender they encode. A format check, not NADRA verification.

Source: https://pakdatakit.dev/docs/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.

> **A format check, not identity verification**
>
> A valid result means the number is well formed. It does not mean the CNIC exists or belongs to anyone. Only NADRA (Verisys) can verify an identity.
## Examples

**TypeScript**

```ts
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"
//   }
```

**Python**

```python
from pakdatakit import validate_cnic, get_cnic_info

validate_cnic('35202-1234567-1')
# → True

validate_cnic('05202-1234567-1')
# → False

get_cnic_info('35202-1234567-1')
# → {
#     "digits": "3520212345671",
#     "formatted": "35202-1234567-1",
#     "province": "Punjab",
#     "gender": "male"
#   }
```

**Dart**

```dart
import 'package:pakdatakit/pakdatakit.dart';

validateCNIC('35202-1234567-1');
// → true

validateCNIC('05202-1234567-1');
// → false

getCNICInfo('35202-1234567-1');
// → {
//     "digits": "3520212345671",
//     "formatted": "35202-1234567-1",
//     "province": "Punjab",
//     "gender": "male"
//   }
```

**C#**

```csharp
using PakDataKit;

Cnic.Validate("35202-1234567-1");
// → true

Cnic.Validate("05202-1234567-1");
// → false

Cnic.GetInfo("35202-1234567-1");
// → {
//     "digits": "3520212345671",
//     "formatted": "35202-1234567-1",
//     "province": "Punjab",
//     "gender": "male"
//   }
```

**PHP**

```php
use PakDataKit\Cnic;

Cnic::validate('35202-1234567-1');
// → true

Cnic::validate('05202-1234567-1');
// → false

Cnic::getInfo('35202-1234567-1');
// → {
//     "digits": "3520212345671",
//     "formatted": "35202-1234567-1",
//     "province": "Punjab",
//     "gender": "male"
//   }
```

## Reference

### validateCNIC()

```ts
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()`](#getcnicinfo), the [CNIC checker](https://pakdatakit.dev/tools/cnic/).

### getCNICInfo()

```ts
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()`](#validatecnic), [`formatCNICInput()`](https://pakdatakit.dev/api/typescript/input/functions/formatcnicinput/) for as-you-type fields.
