Skip to content

Migrating from better-dni

better-dni validates DNIs and NIEs, and generates them. This library covers the same ground, and adds CIFs and K/L/M NIFs, normalization, the reason for each error and localized messages. The comparison on this page is with better-dni 4.4.2.

better-dni nif-dni-nie-cif-validation Notes
isValid(x) isValidNaturalPersonNif(x) Also accepts K/L/M NIFs and the old NIE form. isValidNif(x) also accepts a CIF
isNIF(x) isValidDni(x) Also accepts K/L/M NIFs. validate(x, { types: ["DNI"] }) for a DNI only
isNIE(x) isValidNie(x)
ctrlChar(x) computeControlCharacter(x) Pass the number without its control character
randomNIF(), randomNIE() generateDni(), generateNie() From nif-dni-nie-cif-validation/generate; { seed } for fixed values
randomNIFWith(letter), randomNIEWith(prefix, letter) No equivalent Use generateNie({ prefix }); the check letter follows from the digits
normalize(x) normalize(x) The validators already normalize: no need to call it first

Before, with better-dni:

import { ctrlChar, isNIE, isNIF, isValid, normalize } from "better-dni";
isValid("12345678Z"); // true
isNIF("12345678Z"); // true
isNIE("X1234567L"); // true
ctrlChar("12345678"); // "Z"
isValid(normalize("12345678-z")); // true: normalize first

After:

import {
computeControlCharacter, isValidDni, isValidNaturalPersonNif, isValidNie,
} from "nif-dni-nie-cif-validation";
isValidNaturalPersonNif("12345678Z"); // true
isValidDni("12345678Z"); // true
isValidNie("X1234567L"); // true
computeControlCharacter("12345678"); // "Z"
isValidNaturalPersonNif("12345678-z"); // true: no separate normalize step

To generate numbers, before:

import { randomNIE, randomNIF } from "better-dni";
randomNIF(); // a random DNI
randomNIE(); // a random NIE

After:

import { createGenerator, generateDni, generateNie } from "nif-dni-nie-cif-validation/generate";
generateDni(); // a random DNI
generateNie(); // a random NIE
generateDni({ seed: 1 }); // "62707394X": the same on every run
createGenerator(42).nie(); // "Y4482905N": a seeded stream of values

K/L/M NIFs. better-dni rejects them. They are NIFs of natural persons (Spaniards without a DNI, foreign nationals without an NIE), and this library accepts them in isValidDni and isValidNaturalPersonNif (KLM-1).

The old NIE form. better-dni rejects 10-character NIEs such as X01234567L; they are still valid (NIE-3).

Separators. better-dni accepts lower case, but rejects spaces, dots and hyphens unless you call its normalize() first. Here every validator normalizes, and normalize() also removes dots and adds the leading zeros of a short DNI.

import { isValid, normalize } from "better-dni";
isValid("K1234567L"); // false
isValid("X01234567L"); // false
isValid(" 12345678Z"); // false
normalize(" 12.345.678-z "); // "12.345.678Z": dots are kept
import { isValidNaturalPersonNif, normalize } from "nif-dni-nie-cif-validation";
isValidNaturalPersonNif("K1234567L"); // true
isValidNaturalPersonNif("X01234567L"); // true
isValidNaturalPersonNif(" 12345678Z"); // true
normalize(" 12.345.678-z "); // "12345678Z"

CIFs. better-dni has no CIF validator. Use isValidCif(), or isValidNif() to accept every type.

The reason. better-dni answers yes or no. validate() also gives the type, the canonical form and, for an invalid value, the error code, the SPEC rule and a message in five languages: see Error codes and localized messages.