chile-kit
ES Support

RUT

Validate a Chilean RUT with the modulo-11 verifier digit (dígito verificador), store it in a canonical form and show it formatted. Syntactic check only: no SII lookup.

import { formatRut, formatRutInput, isValidRut, normalizeRut, rutCheckDigit } from "chile-kit/rut";

isValidRut("12.345.678-5");   // true
isValidRut("12345678-K");     // false: wrong verifier
isValidRut("00-0");           // false: passes modulo 11, but no real RUT is all zeros
normalizeRut(" 12.345.678-k "); // "12345678K"  store this
formatRut("123456785");       // "12.345.678-5"  show this
rutCheckDigit("12.345.678");  // "5"  the verifier for a body
isValidRut("1-9", { minBody: 1_000_000 }); // false: too short for a person

Format as the user types

formatRutInput is for a controlled input's onChange: it keeps digits and a final K, adds dots and the dash as the value grows (no dash before the second character) and caps the value at 9 digits plus the verifier. The last character is always treated as the verifier, so validate on blur or submit.

"use client";
import { formatRutInput, isValidRut } from "chile-kit/rut";
import { useState } from "react";

/** Index in `formatted` right after its `count`-th digit or K. */
function caretAfter(formatted: string, count: number): number {
  let seen = 0;
  for (let i = 0; i < formatted.length; i += 1) {
    if (seen === count) return i;
    if (/[0-9K]/.test(formatted[i] ?? "")) seen += 1;
  }
  return formatted.length;
}

export function RutInput() {
  const [rut, setRut] = useState("");
  return (
    <input
      value={rut}
      autoComplete="off"
      aria-invalid={rut.length > 2 && !isValidRut(rut)}
      onChange={(event) => {
        const input = event.target;
        const next = formatRutInput(input.value);
        const typed = input.value.slice(0, input.selectionStart ?? undefined).replace(/[^0-9kK]/g, "");
        input.value = next; // write the DOM first so React keeps the caret
        const caret = caretAfter(next, typed.length);
        input.setSelectionRange(caret, caret);
        setRut(next);
      }}
    />
  );
}

Caret. Reformatting moves the text under the caret. Count the digits before the caret in the raw value and put the caret after the same number of digits in the formatted value, as above; deleting a dot or the dash then just steps over it. A full working form is in examples/next.

Minimum length

One random string in eleven passes modulo 11, so a short typo such as "1-9" or "12.345-6" can look valid. People's RUTs have bodies of about 1.000.000 and up, so for a person field use isValidRut(rut, { minBody: 1_000_000 }). The default stays 1.

API

FunctionReturnsNotes
isValidRut(rut, { minBody? })booleanFactors 2..7, 10 → K, 11 → 0. Body 1 to 9 digits, not all zeros, at least minBody (default 1).
rutCheckDigit(body)string | nullThe verifier "0"-"9" or "K" for a body (dots and spaces allowed); null for an invalid body.
formatRutInput(value)stringProgressive formatting for an input while typing; nullish gives "".
normalizeRut(rut)string | nullStrips dots, dashes and spaces; uppercases K. null when the input is not RUT-shaped (1 to 9 digits, not all zeros, plus a verifier), e.g. "hola" or "00-0".
formatRut(rut)stringXX.XXX.XXX-X. Unparseable input returned as-is; nullish gives "".