Skip to content

fix(isIdentityCard): accept province 8 (AJK) and female gender digit 0 for PK CNIC#2776

Open
chatman-media wants to merge 1 commit into
validatorjs:masterfrom
chatman-media:fix/isidentitycard-pk-cnic
Open

fix(isIdentityCard): accept province 8 (AJK) and female gender digit 0 for PK CNIC#2776
chatman-media wants to merge 1 commit into
validatorjs:masterfrom
chatman-media:fix/isidentitycard-pk-cnic

Conversation

@chatman-media

Copy link
Copy Markdown

Problem

isIdentityCard(str, 'PK') validates Pakistani CNIC numbers (format XXXXX-XXXXXXX-X, 13 digits) with a regex that is stricter than the real CNIC specification in two ways:

const CNIC = /^[1-7][0-9]{4}-[0-9]{7}-[1-9]$/;
  1. First digit (province/administrative-unit code) is limited to 1-7, which rejects province code 8 = Azad Jammu & Kashmir (AJK). Valid CNICs use codes 1-8 (1 = Khyber Pakhtunkhwa, 2 = FATA, 3 = Punjab, 4 = Sindh, 5 = Balochistan, 6 = Islamabad, 7 = Gilgit-Baltistan, 8 = Azad Kashmir).
  2. Last digit (gender) is limited to 1-9, which rejects 0. The final digit encodes gender — odd digits for men, even digits for women — and 0 is a valid even/female digit.

So a real AJK card such as 82345-2345678-7 and a real female card ending in 0 such as 45504-4185771-0 are both incorrectly rejected today.

Fix

const CNIC = /^[1-8][0-9]{4}-[0-9]{7}-[0-9]$/;
  • First digit widened to [1-8] to include AJK (8).
  • Last digit widened to [0-9] to include the female 0 gender digit.

The 5-7-1 digit structure and hyphen placement are unchanged, so previously-invalid inputs (province 0/9, wrong middle-part length, wrong overall length) remain invalid.

Authoritative source

Wikipedia, CNIC (Pakistan)https://en.wikipedia.org/wiki/CNIC_(Pakistan) :

  • Province code 8: > "Azad Kashmir – 8XXXX-XXXXXXX-X"
  • Gender digit: > "The third part following a hyphen (last digit), represents sex of a person. For a man, odd digits i.e. 1, 3, 5, 7, 9 are used, while even digits i.e. 0, 2, 4, 6, 8 are used for women."
  • Structure: > "The first part, which comprises five digits ... has its first digit ... identifying the province or administrative unit of Pakistan." / > "The second and middle part ... consists of seven digits separated by hyphens i.e. XXXXX-1234567-X".

Tests

Added to the PK valid fixtures in test/validators.test.js:

  • 82345-2345678-7, 81234-1234567-3 — province 8 (AJK)
  • 45504-4185771-0, 34543-2323471-0 — female gender digit 0

All four are rejected by the old regex and accepted by the new one. The existing invalid fixtures are unaffected. npm test passes (318 passing, 100% statements/functions/lines) and npm run lint is clean.

@codecov

codecov Bot commented Jun 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (3d2f4b3) to head (b89a0b0).

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #2776   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          114       114           
  Lines         2587      2587           
  Branches       656       656           
=========================================
  Hits          2587      2587           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant