PHPackages                             wasim/pk-cnic-validator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. wasim/pk-cnic-validator

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

wasim/pk-cnic-validator
=======================

A PHP package for validating Pakistan CNIC (Computerized National Identity Card) numbers with and without dashes

v1.1.0(9mo ago)04MITPHPPHP &gt;=7.4

Since Jul 29Pushed 9mo agoCompare

[ Source](https://github.com/MrWasimAbbasi/pk-cnic-validator)[ Packagist](https://packagist.org/packages/wasim/pk-cnic-validator)[ RSS](/packages/wasim-pk-cnic-validator/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (4)Versions (3)Used By (0)

Pakistan CNIC Validator
=======================

[](#pakistan-cnic-validator)

A PHP package for validating Pakistan CNIC (Computerized National Identity Card) numbers with and without dashes, following PSR standards.

Features
--------

[](#features)

- ✅ Validate CNIC numbers with dashes (format: `12345-1234567-1`)
- ✅ Validate CNIC numbers without dashes (format: `1234512345671`)
- ✅ Format CNIC numbers between dash and non-dash formats
- ✅ Extract detailed information from valid CNIC numbers
- ✅ Comprehensive validation rules for Pakistan CNIC structure
- ✅ PSR-12 coding standards compliance
- ✅ Full unit test coverage
- ✅ Static analysis with PHPStan

Installation
------------

[](#installation)

### Via Composer

[](#via-composer)

```
composer require wasim/pk-cnic-validator
```

### Manual Installation

[](#manual-installation)

1. Clone the repository:

```
git clone https://github.com/MrWasimAbbasi/pk-cnic-validator.git
cd pk-cnic-validator
```

2. Install dependencies:

```
composer install
```

Usage
-----

[](#usage)

### Basic Validation

[](#basic-validation)

```
use PkCnicValidator\CnicValidator;

$validator = new CnicValidator();

// Validate CNIC with dashes
$isValid = $validator->isValid('12345-1234567-1'); // true

// Validate CNIC without dashes
$isValid = $validator->isValid('1234512345671'); // true

// Validate invalid CNIC
$isValid = $validator->isValid('invalid'); // false
```

### Specific Format Validation

[](#specific-format-validation)

```
// Check if CNIC is in dash format
$hasDashes = $validator->isValidWithDashes('12345-1234567-1'); // true
$hasDashes = $validator->isValidWithDashes('1234512345671'); // false

// Check if CNIC is in numeric format
$isNumeric = $validator->isValidWithoutDashes('1234512345671'); // true
$isNumeric = $validator->isValidWithoutDashes('12345-1234567-1'); // false
```

### Formatting

[](#formatting)

```
// Format CNIC to include dashes
$withDashes = $validator->formatWithDashes('1234512345671'); // '12345-1234567-1'

// Format CNIC to remove dashes
$withoutDashes = $validator->formatWithoutDashes('12345-1234567-1'); // '1234512345671'

// Invalid CNIC returns null
$formatted = $validator->formatWithDashes('invalid'); // null
```

### Information Extraction

[](#information-extraction)

```
$info = $validator->extractInfo('12345-1234567-1');

// Returns:
[
    'cnic' => '12345-1234567-1',
    'cnic_with_dashes' => '12345-1234567-1',
    'cnic_without_dashes' => '1234512345671',
    'province_code' => '1',
    'district_code' => '12',
    'family_number' => '345',
    'serial_number' => '1234567',
    'check_digit' => '1'
]
```

CNIC Structure
--------------

[](#cnic-structure)

Pakistan CNIC follows this structure:

- **Province Code** (1 digit): 1-9
- **District Code** (2 digits): 11-99
- **Family Number** (3 digits): 001-999
- **Serial Number** (7 digits): 0000001-9999999
- **Check Digit** (1 digit): 0-9

### Valid Formats

[](#valid-formats)

1. **With Dashes**: `12345-1234567-1`
2. **Without Dashes**: `1234512345671`

Testing
-------

[](#testing)

### Run Tests

[](#run-tests)

```
# Run all tests
composer test

# Run tests with coverage
composer test-coverage
```

### Code Quality

[](#code-quality)

```
# Check coding standards
composer cs

# Fix coding standards automatically
composer cs-fix

# Run static analysis
composer stan
```

Requirements
------------

[](#requirements)

- PHP &gt;= 7.4
- Composer

Development
-----------

[](#development)

### Project Structure

[](#project-structure)

```
pk-cnic-validator/
├── src/
│   └── CnicValidator.php
├── tests/
│   ├── CnicValidatorTest.php
│   └── IntegrationTest.php
├── composer.json
├── phpunit.xml
├── phpcs.xml
├── phpstan.neon
└── README.md

```

### Contributing

[](#contributing)

1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Add tests for new functionality
5. Ensure all tests pass
6. Submit a pull request

License
-------

[](#license)

This package is open-sourced software licensed under the [MIT license](LICENSE).

Support
-------

[](#support)

If you encounter any issues or have questions, please open an issue on GitHub.

Changelog
---------

[](#changelog)

### Version 1.0.0

[](#version-100)

- Initial release
- CNIC validation with and without dashes
- Format conversion functionality
- Information extraction
- Comprehensive test coverage

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance61

Regular maintenance activity

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~5 days

Total

2

Last Release

279d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3d23cf1e3f58df6baac5d76a94115498ceea0945897813ae7b30e4f7521c89e8?d=identicon)[WasimAbbasi](/maintainers/WasimAbbasi)

---

Top Contributors

[![MWasimAbbasi](https://avatars.githubusercontent.com/u/197095001?v=4)](https://github.com/MWasimAbbasi "MWasimAbbasi (5 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/wasim-pk-cnic-validator/health.svg)

```
[![Health](https://phpackages.com/badges/wasim-pk-cnic-validator/health.svg)](https://phpackages.com/packages/wasim-pk-cnic-validator)
```

###  Alternatives

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.6k894.0M1.2k](/packages/webmozart-assert)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.5M73](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[ashallendesign/laravel-config-validator

A package for validating your Laravel app's config.

217905.3k5](/packages/ashallendesign-laravel-config-validator)[crazybooot/base64-validation

Laravel validators for base64 encoded files

1341.9M8](/packages/crazybooot-base64-validation)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
