Great topic! Chilean RUT validation is a great case study for region-specific ID systems. Here's a practical breakdown:
RUT Format & Validation Logic
A RUT looks like 12.345.678-9 where the last character is the check digit (0-9 or K). The validation algorithm uses modulo 11:
function validateRUT(rut) {
const clean = rut.replace(/[^0-9kK]/g, '');
if (clean.length < 2) return false;
const body = clean.slice(0, -1);
const dv = clean.slice(-1).toUpperCase();
let sum = 0, multiplier = 2;
for (let i = body.length - 1; i >= 0; i--) {
sum += parseInt(body[i]) * multiplier;
multiplier = multiplier === 7 ? 2 : multiplier + 1;
}
const expected = 11 - (sum % 11);
const dvExpected = expected === 11 ? '0' : expected === 10 ? 'K' : String(expected);
return dv === dvExpected;
}
Libraries to consider:
rutjs (npm) — lightweight, handles formatting + validation@fdograph/rut-utilities — more complete, TypeScript supportvue-rut if you're on Vue.jsBest Practices for Scalable Apps:
laralib/l5-brasil or building a shared validation service if you're multi-country.Happy to share more on the multi-country architecture if that's relevant to your project!