JSPM

idnumbers

1.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1016
  • Score
    100M100P100Q86546F
  • License MIT

A TypeScript library for verifying and parsing national ID numbers - supports 80 countries across 6 continents including USA, UK, France, Germany, Japan, China, India, Brazil, and many more

Package Exports

  • idnumbers
  • idnumbers/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (idnumbers) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

idnumbers

A comprehensive TypeScript/JavaScript library for validating and parsing national identification numbers from 80 countries across 6 continents.

npm version License: MIT TypeScript

Features

  • โœ… 80 countries supported - Comprehensive coverage across all continents
  • ๐Ÿ” Validation - Verify ID number format and checksums
  • ๐Ÿ“Š Parsing - Extract information like birth date, gender, and citizenship
  • ๐Ÿ›ก๏ธ Type-safe - Full TypeScript support with type definitions
  • ๐Ÿ“ฆ Zero dependencies - Lightweight and secure
  • โœจ Well-tested - Comprehensive test coverage with 100% pass rate
  • ๐ŸŒ Multiple formats - Supports various ID number formats per country

Installation

npm install idnumbers
yarn add idnumbers
pnpm add idnumbers

Quick Start

import { validateNationalId, parseIdInfo } from 'idnumbers';

// Validate a US Social Security Number
const result = validateNationalId('USA', '123-45-6789');
console.log(result.isValid); // true or false

// Parse information from a South African ID
const info = parseIdInfo('ZAF', '8001015009087');
console.log(info);
// {
//   yyyymmdd: Date(1980-01-01),
//   gender: 'male',
//   citizenship: 'citizen',
//   ...
// }

API Reference

validateNationalId(countryCode, idNumber)

Validates a national ID number for a specific country.

Parameters:

  • countryCode (string): ISO 3166-1 alpha-3 country code (e.g., 'USA', 'GBR', 'FRA')
  • idNumber (string): The ID number to validate

Returns: ValidationResult

{
  isValid: boolean;
  countryCode: string;
  idNumber: string;
  extractedInfo?: any;
  errorMessage?: string;
}

Example:

const result = validateNationalId('GBR', 'AB123456C');
if (result.isValid) {
  console.log('Valid UK National Insurance Number');
} else {
  console.log('Invalid:', result.errorMessage);
}

parseIdInfo(countryCode, idNumber)

Extracts information from a national ID number (if supported by the country).

Parameters:

  • countryCode (string): ISO 3166-1 alpha-3 country code
  • idNumber (string): The ID number to parse

Returns: ParsedInfo | null

The returned object varies by country but commonly includes:

  • yyyymmdd or birthDate: Date of birth
  • gender: 'male' or 'female'
  • citizenship: 'citizen' or 'resident'
  • location or region: Geographic information
  • Additional country-specific fields

Example:

const info = parseIdInfo('SWE', '811218-9876');
console.log(info);
// {
//   yyyymmdd: Date(1981-12-18),
//   gender: 'male',
//   ...
// }

validateMultipleIds(ids)

Validates multiple ID numbers in batch.

Parameters:

  • ids (Array): Array of objects with countryCode and idNumber

Returns: Array of ValidationResult

Example:

const results = validateMultipleIds([
  { countryCode: 'USA', idNumber: '123-45-6789' },
  { countryCode: 'GBR', idNumber: 'AB123456C' },
  { countryCode: 'FRA', idNumber: '255081416802538' }
]);

results.forEach(result => {
  console.log(`${result.countryCode}: ${result.isValid}`);
});

listSupportedCountries()

Returns a list of all supported countries.

Returns: Array of country information

[
  {
    code: 'USA',
    name: 'United States',
    idType: 'Social Security Number'
  },
  ...
]

Example:

const countries = listSupportedCountries();
console.log(`Supports ${countries.length} countries`);

getCountryIdFormat(countryCode)

Gets format information for a country's ID number.

Parameters:

  • countryCode (string): ISO 3166-1 alpha-3 country code

Returns: Format information or null

Example:

const format = getCountryIdFormat('IND');
console.log(format);
// {
//   countryCode: 'IND',
//   format: 'XXXX XXXX XXXX',
//   length: { min: 12, max: 12 },
//   ...
// }

Supported Countries

North America (3)

  • ๐Ÿ‡บ๐Ÿ‡ธ USA - Social Security Number (SSN)
  • ๐Ÿ‡จ๐Ÿ‡ฆ CAN - Social Insurance Number (SIN)
  • ๐Ÿ‡ฒ๐Ÿ‡ฝ MEX - CURP (Clave รšnica de Registro de Poblaciรณn)

South America (4)

  • ๐Ÿ‡ฆ๐Ÿ‡ท ARG - DNI (Documento Nacional de Identidad)
  • ๐Ÿ‡ง๐Ÿ‡ท BRA - CPF (Cadastro de Pessoas Fรญsicas)
  • ๐Ÿ‡จ๐Ÿ‡ฑ CHL - RUT/RUN (Rol รšnico Tributario)
  • ๐Ÿ‡ป๐Ÿ‡ช VEN - Cรฉdula de Identidad

Europe (38)

  • ๐Ÿ‡ฆ๐Ÿ‡ฑ ALB - National ID Number
  • ๐Ÿ‡ฆ๐Ÿ‡น AUT - Social Security Number
  • ๐Ÿ‡ง๐Ÿ‡ช BEL - National Register Number
  • ๐Ÿ‡ง๐Ÿ‡ฆ BIH - JMBG (Unique Master Citizen Number)
  • ๐Ÿ‡ง๐Ÿ‡ฌ BGR - Personal Number (EGN)
  • ๐Ÿ‡ญ๐Ÿ‡ท HRV - Personal Identification Number (OIB)
  • ๐Ÿ‡จ๐Ÿ‡พ CYP - Tax Identification Number
  • ๐Ÿ‡จ๐Ÿ‡ฟ CZE - Birth Number
  • ๐Ÿ‡ฉ๐Ÿ‡ฐ DNK - CPR Number
  • ๐Ÿ‡ช๐Ÿ‡ช EST - Personal Identification Code
  • ๐Ÿ‡ซ๐Ÿ‡ฎ FIN - Personal Identity Code (HETU)
  • ๐Ÿ‡ซ๐Ÿ‡ท FRA - Social Security Number (NIR)
  • ๐Ÿ‡ฉ๐Ÿ‡ช DEU - Tax ID (Steueridentifikationsnummer)
  • ๐Ÿ‡ฌ๐Ÿ‡ท GRC - Tax Registration Number (AFM)
  • ๐Ÿ‡ญ๐Ÿ‡บ HUN - Tax Number
  • ๐Ÿ‡ฎ๐Ÿ‡ธ ISL - National ID (Kennitala)
  • ๐Ÿ‡ฎ๐Ÿ‡ช IRL - Personal Public Service Number (PPS)
  • ๐Ÿ‡ฎ๐Ÿ‡น ITA - Fiscal Code (Codice Fiscale)
  • ๐Ÿ‡ฑ๐Ÿ‡ป LVA - Personal Code
  • ๐Ÿ‡ฑ๐Ÿ‡น LTU - Personal Code
  • ๐Ÿ‡ฑ๐Ÿ‡บ LUX - National ID Number
  • ๐Ÿ‡ฒ๐Ÿ‡ฐ MKD - JMBG (Unique Master Citizen Number)
  • ๐Ÿ‡ฒ๐Ÿ‡ช MNE - JMBG (Unique Master Citizen Number)
  • ๐Ÿ‡ณ๐Ÿ‡ฑ NLD - BSN (Burgerservicenummer)
  • ๐Ÿ‡ณ๐Ÿ‡ด NOR - National Identity Number
  • ๐Ÿ‡ต๐Ÿ‡ฑ POL - PESEL
  • ๐Ÿ‡ต๐Ÿ‡น PRT - NIF (Nรบmero de Identificaรงรฃo Fiscal)
  • ๐Ÿ‡ท๐Ÿ‡ด ROU - Personal Numerical Code (CNP)
  • ๐Ÿ‡ท๐Ÿ‡บ RUS - Internal Passport
  • ๐Ÿ‡ท๐Ÿ‡ธ SRB - JMBG (Unique Master Citizen Number)
  • ๐Ÿ‡ธ๐Ÿ‡ฐ SVK - Birth Number
  • ๐Ÿ‡ธ๐Ÿ‡ฎ SVN - Personal Number (EMล O)
  • ๐Ÿ‡ช๐Ÿ‡ธ ESP - DNI/NIE
  • ๐Ÿ‡ธ๐Ÿ‡ช SWE - Personal Identity Number (Personnummer)
  • ๐Ÿ‡จ๐Ÿ‡ญ CHE - Social Security Number (AHV-Nr)
  • ๐Ÿ‡น๐Ÿ‡ท TUR - TC Kimlik No
  • ๐Ÿ‡บ๐Ÿ‡ฆ UKR - Tax Number (RNTRC)
  • ๐Ÿ‡ฌ๐Ÿ‡ง GBR - National Insurance Number (NINO)

Asia (18)

  • ๐Ÿ‡ง๐Ÿ‡ญ BHR - Personal Number (CPR)
  • ๐Ÿ‡ง๐Ÿ‡ฉ BGD - National ID
  • ๐Ÿ‡จ๐Ÿ‡ณ CHN - Resident Identity Card
  • ๐Ÿ‡ฌ๐Ÿ‡ช GEO - Personal Number
  • ๐Ÿ‡ญ๐Ÿ‡ฐ HKG - Hong Kong Identity Card
  • ๐Ÿ‡ฎ๐Ÿ‡ณ IND - Aadhaar
  • ๐Ÿ‡ฎ๐Ÿ‡ฉ IDN - NIK (Nomor Induk Kependudukan)
  • ๐Ÿ‡ฏ๐Ÿ‡ต JPN - My Number
  • ๐Ÿ‡ฐ๐Ÿ‡ฟ KAZ - Individual Identification Number (IIN)
  • ๐Ÿ‡ฐ๐Ÿ‡ท KOR - Resident Registration Number
  • ๐Ÿ‡ฐ๐Ÿ‡ผ KWT - Civil ID
  • ๐Ÿ‡ฑ๐Ÿ‡ฐ LKA - National Identity Card
  • ๐Ÿ‡ฒ๐Ÿ‡พ MYS - MyKad
  • ๐Ÿ‡ต๐Ÿ‡ฐ PAK - CNIC (Computerized National Identity Card)
  • ๐Ÿ‡ต๐Ÿ‡ญ PHL - PhilSys Number
  • ๐Ÿ‡ธ๐Ÿ‡ฆ SAU - National ID
  • ๐Ÿ‡ธ๐Ÿ‡ฌ SGP - NRIC/FIN
  • ๐Ÿ‡น๐Ÿ‡ญ THA - National ID
  • ๐Ÿ‡น๐Ÿ‡ผ TWN - National Identification Card
  • ๐Ÿ‡ป๐Ÿ‡ณ VNM - Citizen Identity Card

Africa (3)

  • ๐Ÿ‡ณ๐Ÿ‡ฌ NGA - National Identification Number (NIN)
  • ๐Ÿ‡ฟ๐Ÿ‡ฆ ZAF - ID Number
  • ๐Ÿ‡ฟ๐Ÿ‡ผ ZWE - National ID

Oceania (2)

  • ๐Ÿ‡ฆ๐Ÿ‡บ AUS - Medicare Number
  • ๐Ÿ‡ณ๐Ÿ‡ฟ NZL - Driver License Number

Middle East (Additional)

  • ๐Ÿ‡ฆ๐Ÿ‡ช ARE - Emirates ID
  • ๐Ÿ‡ฎ๐Ÿ‡ฑ ISR - ID Number (Teudat Zehut)

Usage Examples

Basic Validation

import { validateNationalId } from 'idnumbers';

// US Social Security Number
const usa = validateNationalId('USA', '123-45-6789');
console.log(usa.isValid); // true

// UK National Insurance Number
const uk = validateNationalId('GBR', 'AB123456C');
console.log(uk.isValid); // true

// French Social Security Number
const france = validateNationalId('FRA', '255081416802538');
console.log(france.isValid); // true

Parsing Information

import { parseIdInfo } from 'idnumbers';

// South Africa - Extract birth date, gender, citizenship
const zaf = parseIdInfo('ZAF', '8001015009087');
console.log(zaf);
// {
//   yyyymmdd: Date(1980-01-01),
//   gender: 'male',
//   citizenship: 'citizen'
// }

// Sweden - Extract birth date and gender
const swe = parseIdInfo('SWE', '811218-9876');
console.log(swe);
// {
//   yyyymmdd: Date(1981-12-18),
//   gender: 'male'
// }

// China - Extract birth date, province, and gender
const chn = parseIdInfo('CHN', '11010219840406970X');
console.log(chn);
// {
//   birthDate: Date(1984-04-06),
//   province: 'Beijing',
//   gender: 'male'
// }

Batch Validation

import { validateMultipleIds } from 'idnumbers';

const ids = [
  { countryCode: 'USA', idNumber: '123-45-6789' },
  { countryCode: 'GBR', idNumber: 'AB123456C' },
  { countryCode: 'JPN', idNumber: '123456789012' },
  { countryCode: 'XXX', idNumber: '123' } // Invalid country
];

const results = validateMultipleIds(ids);
results.forEach((result, index) => {
  console.log(`ID ${index + 1}: ${result.isValid ? 'Valid' : 'Invalid'}`);
  if (!result.isValid) {
    console.log(`  Error: ${result.errorMessage}`);
  }
});

Error Handling

import { validateNationalId } from 'idnumbers';

const result = validateNationalId('USA', '000-45-6789');
if (!result.isValid) {
  console.log('Validation failed:', result.errorMessage);
  // "Validation failed: Invalid SSN - forbidden prefix 000"
}

// Unsupported country
const invalid = validateNationalId('XXX', '123456789');
console.log(invalid.errorMessage);
// "Unsupported country code: XXX"

TypeScript Usage

import { validateNationalId, parseIdInfo, ValidationResult } from 'idnumbers';

// Type-safe validation
const result: ValidationResult = validateNationalId('USA', '123-45-6789');

if (result.isValid && result.extractedInfo) {
  // extractedInfo is typed based on the country
  console.log('Valid ID with extracted info:', result.extractedInfo);
}

// Type-safe parsing
const info = parseIdInfo('ZAF', '8001015009087');
if (info) {
  // TypeScript knows the possible fields
  console.log('Birth date:', info.yyyymmdd);
  console.log('Gender:', info.gender);
}

Integration with Forms

import { validateNationalId } from 'idnumbers';

function validateUserID(country: string, idNumber: string): {
  valid: boolean;
  message: string;
} {
  const result = validateNationalId(country, idNumber);

  return {
    valid: result.isValid,
    message: result.isValid
      ? 'Valid ID number'
      : result.errorMessage || 'Invalid ID number'
  };
}

// In your form handler
const validation = validateUserID('USA', userInput);
if (!validation.valid) {
  showError(validation.message);
}

Country-Specific Notes

United States (USA)

  • Format: XXX-XX-XXXX (with or without dashes)
  • Forbidden prefixes: 000, 666, 900-999
  • Example: 123-45-6789

United Kingdom (GBR)

  • Format: Two letters, six digits, one letter
  • Forbidden prefixes: BG, GB, NK, KN, TN, NT, ZZ
  • Example: AB123456C

China (CHN)

  • Format: 18 digits (17 digits + checksum)
  • Contains: Region code, birth date, sequence number, checksum
  • Checksum can be X (representing 10)
  • Example: 11010219840406970X

South Africa (ZAF)

  • Format: 13 digits
  • Contains: Birth date (YYMMDD), gender, citizenship
  • Example: 8001015009087

France (FRA)

  • Format: 15 digits (Social Security Number)
  • Contains: Gender, year/month of birth, department code
  • Example: 255081416802538

Germany (DEU)

  • Format: 11 digits (Tax ID)
  • Contains: Random number with checksum validation
  • Example: 65929970489

Testing

The library includes comprehensive test coverage with 301 tests covering:

  • Format validation
  • Checksum verification
  • Edge cases and error handling
  • Information extraction
  • Cross-country consistency

Run tests:

npm test

Run tests with coverage:

npm run test:coverage

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Development Setup

  1. Clone the repository:

    git clone https://github.com/identique/idnumbers-npm.git
    cd idnumbers-npm
  2. Install dependencies:

    npm install

    This automatically sets up pre-commit hooks via Husky.

  3. Pre-commit Hooks:

    The following checks run automatically before each commit:

    • โœจ Prettier - Code formatting on staged files
    • ๐Ÿ”ง TypeScript - Type checking and compilation
    • ๐Ÿงช Tests - Full test suite

    If any check fails, the commit will be blocked until fixed.

  4. Run tests:

    npm test                # Run all tests
    npm run test:coverage   # Run with coverage report
  5. Build:

    npm run build          # Compile TypeScript
  6. Linting:

    npm run lint           # Check for issues
    npm run lint:fix       # Auto-fix issues

License

MIT License - see LICENSE file for details

Changelog

2.1.0

  • Added support for 80 countries
  • Comprehensive test coverage
  • Full TypeScript support
  • Enhanced validation and parsing capabilities

Acknowledgments

This library is inspired by and maintains compatibility with validation logic from various national ID systems worldwide.

Support

For issues, questions, or contributions, please visit:

See Also