惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
L
LangChain Blog
GbyAI
GbyAI
F
Fortinet All Blogs
腾讯CDC
Last Week in AI
Last Week in AI
A
About on SuperTechFans
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - Franky
B
Blog
D
Docker
G
Google Developers Blog
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
宝玉的分享
宝玉的分享
U
Unit 42
Blog — PlanetScale
Blog — PlanetScale
B
Blog RSS Feed

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Vanilla JavaScript validators: the algorithms behind Span...
Livi · 2026-04-30 · via DEV Community

Livi

Vanilla JavaScript validators: the algorithms behind Spanish ID documents

This article walks through the validation algorithms for Spanish identity documents (DNI, NIE, CIF) and their Iberoamerican counterparts (RFC, RUT, RUC, NIT, CUIT). All implementations are vanilla JavaScript with zero dependencies. They run client-side at haz.tools/c/validar.

Why these algorithms matter

Identity documents in Spanish-speaking countries embed mathematical structure to detect transcription errors. A single typo in a DNI almost always produces an invalid letter; the validator catches it instantly. Validating before submission prevents the most common error class in any form that captures these documents.

Most importantly, this validation does not require contacting any government API. The math is enough.

DNI (Spain)

const DNI_LETTERS = 'TRWAGMYFPDXBNJZSQVHLCKE';

export function validateDNI(input) {
  const match = input.toUpperCase().replace(/\s/g, '').match(/^([0-9]{8})([A-Z])$/);
  if (!match) return { valid: false, reason: 'format' };
  const number = parseInt(match[1], 10);
  const expected = DNI_LETTERS[number % 23];
  return {
    valid: match[2] === expected,
    expected,
    received: match[2]
  };
}

Enter fullscreen mode Exit fullscreen mode

The letter is not arbitrary. It is 'TRWAGMYFPDXBNJZSQVHLCKE'[number % 23]. The string has 23 characters. I, Ñ, O, U are intentionally excluded to avoid confusion with similar-looking digits and letters.

NIE (Spain — foreign residents)

const NIE_PREFIX = { X: '0', Y: '1', Z: '2' };

export function validateNIE(input) {
  const match = input.toUpperCase().replace(/\s/g, '').match(/^([XYZ])([0-9]{7})([A-Z])$/);
  if (!match) return { valid: false, reason: 'format' };
  const numberStr = NIE_PREFIX[match[1]] + match[2];
  const number = parseInt(numberStr, 10);
  const expected = DNI_LETTERS[number % 23];
  return { valid: match[3] === expected, expected, received: match[3] };
}

Enter fullscreen mode Exit fullscreen mode

Same modulo 23, but the leading letter is mapped to a digit first. NIE is the document issued to non-Spanish residents and to foreigners with tax obligations in Spain.

CIF (Spain — corporate)

const CIF_LETTERS = 'JABCDEFGHI';

export function validateCIF(input) {
  const match = input.toUpperCase().replace(/\s/g, '').match(/^([ABCDEFGHJKLMNPQRSUVW])([0-9]{7})([0-9A-J])$/);
  if (!match) return { valid: false, reason: 'format' };

  const [_, type, digits, control] = match;
  let evenSum = 0, oddSum = 0;

  for (let i = 0; i < digits.length; i++) {
    const d = parseInt(digits[i], 10);
    if (i % 2 === 0) {
      const doubled = d * 2;
      oddSum += Math.floor(doubled / 10) + (doubled % 10);
    } else {
      evenSum += d;
    }
  }

  const total = evenSum + oddSum;
  const controlDigit = (10 - (total % 10)) % 10;

  // Some entity types use a digit, others a letter
  const useLetterControl = 'KPQSNW'.includes(type);
  const expected = useLetterControl ? CIF_LETTERS[controlDigit] : controlDigit.toString();

  return { valid: control === expected, expected, received: control };
}

Enter fullscreen mode Exit fullscreen mode

The leading letter encodes entity type (A=SA, B=SL, etc.). The control character can be a digit or a letter depending on the type.

IBAN (módulo 97, ISO 13616)

export function validateIBAN(input) {
  const iban = input.toUpperCase().replace(/\s/g, '');
  if (!/^[A-Z]{2}[0-9]{2}[A-Z0-9]+$/.test(iban)) return false;

  const reordered = iban.slice(4) + iban.slice(0, 4);
  const numeric = reordered.replace(/[A-Z]/g, c =>
    (c.charCodeAt(0) - 55).toString()
  );

  let remainder = 0;
  for (const digit of numeric) {
    remainder = (remainder * 10 + parseInt(digit, 10)) % 97;
  }

  return remainder === 1;
}

Enter fullscreen mode Exit fullscreen mode

The block-by-block modulo is critical. The substituted IBAN exceeds Number.MAX_SAFE_INTEGER (2^53 - 1). A naive Number(numeric) % 97 returns wrong results for any Spanish IBAN.

Alternative: BigInt(numeric) % 97n === 1n. Cleaner but slightly slower. The block approach is faster and works in environments where BigInt is unavailable.

Live demo at haz.tools.

RFC (Mexico)

export function validateRFC(input) {
  // Persona física: 13 chars (4 letters + 6 digits + 3 chars)
  // Persona moral: 12 chars (3 letters + 6 digits + 3 chars)
  const rfc = input.toUpperCase().replace(/\s/g, '');
  return /^[A-ZÑ&]{3,4}[0-9]{6}[A-Z0-9]{3}$/.test(rfc);
}

Enter fullscreen mode Exit fullscreen mode

The 6 digits are the date in YYMMDD format. The last 3 characters are a homoclave assigned by SAT. There is no checksum to verify in pure form; structure validation is the maximum.

RUT (Chile, módulo 11)

export function validateRUT(input) {
  const cleaned = input.replace(/[.-]/g, '').toUpperCase();
  const match = cleaned.match(/^(\d+)([0-9K])$/);
  if (!match) return false;

  const [_, body, dv] = match;
  let sum = 0;
  let multiplier = 2;

  for (let i = body.length - 1; i >= 0; i--) {
    sum += parseInt(body[i], 10) * multiplier;
    multiplier = multiplier === 7 ? 2 : multiplier + 1;
  }

  const expected = 11 - (sum % 11);
  const expectedDV = expected === 11 ? '0' : expected === 10 ? 'K' : expected.toString();
  return dv === expectedDV;
}

Enter fullscreen mode Exit fullscreen mode

Chilean RUT uses módulo 11 with a digit verifier. The verifier can be a digit (0-9) or 'K' when the result is 10.

Credit cards (Luhn)

export function validateLuhn(input) {
  const digits = input.replace(/\D/g, '');
  if (digits.length < 12 || digits.length > 19) return false;

  let sum = 0;
  let alternate = false;

  for (let i = digits.length - 1; i >= 0; i--) {
    let n = parseInt(digits[i], 10);
    if (alternate) {
      n *= 2;
      if (n > 9) n -= 9;
    }
    sum += n;
    alternate = !alternate;
  }

  return sum % 10 === 0;
}

Enter fullscreen mode Exit fullscreen mode

Luhn detects almost all single-digit errors and most adjacent-digit transpositions. Combined with prefix-based network detection (Visa starts with 4, Mastercard with 51-55 or 2221-2720, Amex with 34/37, etc.) it covers credit card validation needs.

Production usage

All these validators ship in haz.tools/c/validar and run client-side. No data leaves the browser. The implementations above are essentially what is in production, modulo error formatting and edge case handling.

Other validators in the same family at haz.tools:

  • IBAN for SEPA countries
  • RUC (Peru), NIT (Colombia), CUIT (Argentina)
  • Email with MX record check
  • URL with structure validation
  • JSON Schema validator
  • Regex tester with match highlighting

The full directory has 100+ tools. If you need a validator that does not exist, request it: contacto@haz.tools.