npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

validar-cif

v1.0.0

Published

Valida CIF, DNI y NIE españoles (formato y dígito de control). Sin dependencias.

Readme

validar-cif

tests

npm PyPI Packagist Licencia MIT

Valida CIF, DNI y NIE españoles (formato y carácter de control) en JavaScript, Python y PHP. Sin dependencias, con tests y con el mismo resultado en los tres lenguajes.

Mantenido por APIEmpresas.es. Es el mismo algoritmo que usa el validador de CIF online.

Un CIF bien formado no garantiza que la empresa exista. La fórmula es pública y un número inventado puede cuadrar. Para saber si hay una sociedad con ese CIF, su razón social y si sigue activa, usa la API de empresas (100 consultas gratis, sin tarjeta) o el buscador de empresas por CIF.

JavaScript / Node.js

npm install validar-cif
const { validarCif, validarNif, tipoNif, tipoEntidad } = require('validar-cif');

validarCif('A46103834');      // true
validarCif('a-46.103.834');   // true (limpia guiones, puntos, espacios y minúsculas)
validarCif('A46103835');      // false: el control debería ser 4
validarNif('12345678Z');      // true (CIF, DNI o NIE)
tipoNif('X1234567L');         // 'nie'
tipoEntidad('B00000000');     // 'Sociedad de responsabilidad limitada'

Incluye tipos de TypeScript.

Python

pip install validar-cif
from validar_cif import validar_cif, validar_nif, tipo_nif, tipo_entidad

validar_cif("A46103834")   # True
validar_nif("12345678Z")   # True
tipo_nif("X1234567L")      # 'nie'
tipo_entidad("A46103834")  # 'Sociedad anónima'

PHP

composer require apiempresas/validar-cif
use ApiEmpresas\ValidarCif\Validador;

Validador::cif('A46103834');          // true
Validador::nif('12345678Z');          // true
Validador::tipo('X1234567L');         // 'nie'
Validador::tipoEntidad('A46103834');  // 'Sociedad anónima'

Funciones

| JavaScript | Python | PHP | Devuelve | |---|---|---|---| | validarCif | validar_cif | Validador::cif | true si el CIF de empresa está bien formado | | validarDni | validar_dni | Validador::dni | true si la letra del DNI es correcta | | validarNie | validar_nie | Validador::nie | true si la letra del NIE es correcta | | validarNif | validar_nif | Validador::nif | true si es CIF, DNI o NIE válido | | tipoNif | tipo_nif | Validador::tipo | 'cif', 'dni', 'nie' o nulo | | tipoEntidad | tipo_entidad | Validador::tipoEntidad | Forma jurídica según la letra del CIF | | limpiar | limpiar | Validador::limpiar | El valor sin espacios, guiones, puntos ni barras, en mayúsculas |

Cómo se calcula el control del CIF

Un CIF tiene 9 caracteres: una letra con la forma jurídica, 7 números y un carácter de control.

  1. Suma los números en posición par (2.º, 4.º y 6.º).
  2. Multiplica por 2 los de posición impar (1.º, 3.º, 5.º y 7.º); si sale de dos cifras, súmalas (7 × 2 = 14 → 5).
  3. Suma los dos totales. Si la última cifra es 0, el control es 0; si no, 10 menos esa cifra.
  4. Si el control va en letra, es la de esa posición en JABCDEFGHI.

Las letras A, B, E y H llevan control numérico; N, P, Q, R, S y W, letra; el resto, cualquiera de los dos. Ejemplo con A46103834: pares 6 + 0 + 8 = 14; impares 8 + 2 + 6 + 6 = 22; total 36 → control 10 − 6 = 4.

En el DNI, la letra es la del resto de dividir el número entre 23 en TRWAGMYFPDXBNJZSQVHLCKE. En el NIE se cambia la X por 0, la Y por 1 y la Z por 2.

Más detalle y ejemplos (formularios, Laravel, Excel): Validar CIF en PHP, Python y JavaScript y fórmula para Excel y Google Sheets.

Comprobar si la empresa existe

El formato solo dice que el número está bien escrito. Para saber si la empresa existe, su razón social oficial y si sigue activa, una llamada a la API:

curl "https://apiempresas.es/api/v1/companies?cif=A46103834" -H "X-API-KEY: TU_API_KEY"

Devuelve razón social, estado (activa, en concurso, extinguida…), CNAE, provincia y, en el plan Pro, administradores y domicilio. Para comprobar toda la lista de clientes antes de facturar (Verifactu), POST /api/v1/companies/verify/batch revisa hasta 1.000 NIF por petición.

Herramientas gratis sin programar: validar CIF, comprobar NIF, DNI o NIE, buscar empresa por CIF y comprobar el NIF de tus clientes antes de facturar.

Tests

node js/test.js
cd python && python -m unittest discover -s tests
composer install && composer test

English

Validates Spanish tax IDs (CIF for companies, DNI and NIE for individuals) in JavaScript, Python and PHP, with no dependencies. To check whether a company actually exists and is active, use the Spanish company data API.

Licencia

MIT