
I built a zero-dependency TypeScript library to validate Spanish DNI, NIE and CIF
If you've ever built a form in Spain, you know the pain: validating a DNI is straightforward, but then someone enters a NIE, or a company CIF, and suddenly you're copy-pasting regex from a 2009 Stack Overflow answer. I got tired of that. So I built @polgubau/validar-dni — a tiny, typed, zero-dependency library that handles all three. What it does import { validDniCifNie , parseDni } from " @polgubau/validar-dni " ; // Simple boolean check validDniCifNie ( " 12345678Z " ); // true validDniCifNie ( " 12345678A " ); // false // Rich result — tells you what's wrong parseDni ( " 12345678A " ); // { // isValid: false, // type: "NIF", // normalized: "12345678A", // expectedControl: "Z" ← the correct letter // } It covers: Type Format Example NIF (DNI) 8 digits + letter 12345678Z NIE X/Y/Z + 7 digits + letter X1234567L CIF Org letter + 7 digits + letter/digit A58818501 NIE especial T + 8 digits T12345678 Why not just use a regex? A regex can check the format . But Spanish identifiers have a co
Continue reading on Dev.to JavaScript
Opens in a new tab



