validator-tax-id
v1.3.0
Published
A lightweight, zero-dependency, and universal TypeScript library to validate Tax IDs (Identification Numbers)
Downloads
42
Readme
Tax ID Validator
A lightweight, zero-dependency, and universal TypeScript library to validate Tax IDs (Identification Numbers)
It uses precise mathematical algorithms to verify the integrity of the document number and follow guides of the country governments
Supported Countries
| Country | Code | Documents Supported | Algorithm | | ----------- | ---- | ------------------------------ | ------------------ | | 🇪🇸 Spain | es | DNI, NIE, CIF | Module 23 | | 🇵🇹 Portugal | pt | NIF (Personal) | Module 11 | | 🇫🇷 France | fr | SIREN, SIRET, NIR | Luhn + Mod.97 | | 🇩🇪 Germany | de | SteuerIdNr, VAT Number, W-IdNr | ISO 7064 Mod 10,11 |
Features
- 🚀 Lightweight: Zero external dependencies.
- 🔒 Type-Safe: Written in TypeScript with full type definitions.
- 🌍 Universal: Works in Node.js, React, Vue, Next.js, and Browsers (Legacy Script Tag).
- ⚡ Tree-shakeable: Only import what you need (if using advanced exports).
Installation
npm install validator-tax-id
# or
yarn add validator-tax-id
# or
pnpm add validator-tax-idUsage
Basic Usage
The main function validateIdentification takes two arguments: the country code (ISO 3166-1 alpha-2) and the value to validate.
import { validateIdentification } from "validator-tax-id";
// 🇪🇸 Spain (ES)
validateIdentification("es", "12345678Z"); // true (DNI)
validateIdentification("es", "X1234567L"); // true (NIE)
validateIdentification("es", "A58818501"); // true (CIF)
// 🇵🇹 Portugal (PT)
validateIdentification("pt", "232013969"); // true (NIF)
// 🇫🇷 France (FR)
validateIdentification("fr", "443061841"); // true (SIREN)
// 🇩🇪 Germany (DE)
validateIdentification("de", "86095742719"); // true (SteuerIdNr)
validateIdentification("de", "DE136695976"); // true (VAT Number)Individual Validators (Recommended) ✨
For better tree-shaking and direct access, use individual validators:
import {
// Spain
validateDNI,
validateNIE,
validateCIF,
// France
validateSIREN,
validateSIRET,
validateNIR,
// Portugal
validateNIF,
// Germany
validateSteuerIdNr,
validateVatNumber,
validateWidnr,
} from "validator-tax-id";
// 🇪🇸 Spain - Direct validation
validateDNI("12345678Z"); // true
validateNIE("X1234567L"); // true
validateCIF("A58818501"); // true
// 🇫🇷 France - Direct validation
validateSIREN("443061841"); // true
validateSIRET("44306184100047"); // true
validateNIR("188057512301180"); // true
// 🇵🇹 Portugal - Direct validation
validateNIF("123456789"); // true
// 🇩🇪 Germany - Direct validation
validateSteuerIdNr("86095742719"); // true
validateVatNumber("DE136695976"); // true
validateWidnr("136695976"); // trueCountry Auto-detect
If you don't know the specific document type:
import {
validateES,
validateFR,
validatePT,
validateDE,
} from "validator-tax-id";
validateES("12345678Z"); // true (auto-detects DNI)
validateES("A58818501"); // true (auto-detects CIF)
validateFR("443061841"); // true (auto-detects SIREN)
validatePT("123456789"); // true
validateDE("86095742719"); // true (auto-detects SteuerIdNr)
validateDE("DE136695976"); // true (auto-detects VAT Number)API Reference
validateIdentification(country, value)
- country:
CountryCode('es' | 'pt' | 'fr' | 'de') - The ISO code of the country. - value:
string- The document string to validate. - Returns:
boolean(trueif valid,falseotherwise).
Note: The validator sanitizes the input automatically (removes spaces, hyphens, and is case-insensitive).
Support
If this library helped you, consider buying me a coffee ☕
License
MIT
