Skip to content

calculateVerifier()

Calculates the verifier digit for a given RUT body using the Modulo 11 algorithm.

Basic Usage#

TypeScript
import { calculateVerifier } from 'rut.ts'
 
calculateVerifier('12345678') // '5'
calculateVerifier('24657622') // 'K'

Type Signature#

TypeScript
function calculateVerifier(rutBody: string): VerifierDigit
function calculateVerifier(rutBody: string, options: { throwOnError: false }): VerifierDigit | null
function calculateVerifier(rutBody: string, options: { throwOnError: true }): VerifierDigit
 
type VerifierDigit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | 'K'

Parameters#

  • rutBody (string) - The RUT body (7-8 digits, without verifier)
  • options (optional) - Configuration options
    • throwOnError (boolean, default: true) - If false, returns null instead of throwing

Return Value#

Returns VerifierDigit or null:

  • A single character: '0' to '9' or 'K'
  • null if invalid (only in safe mode)

Algorithm#

The function implements the Modulo 11 algorithm:

  1. Take the RUT body digits
  2. Multiply each digit by factors (2,3,4,5,6,7,2,3,4...) from right to left
  3. Sum all products
  4. Calculate 11 - (sum % 11)
  5. If result is 11, verifier is '0'
  6. If result is 10, verifier is 'K'
  7. Otherwise, verifier is the result as a string

Examples#

Basic Calculation#

TypeScript
import { calculateVerifier } from 'rut.ts'
 
// Numeric verifiers
calculateVerifier('12345678') // '5'
calculateVerifier('18972631') // '7'
calculateVerifier('11111111') // '1'
 
// K verifier (when result is 10)
calculateVerifier('24657622') // 'K'
calculateVerifier('14625621') // 'K'

Building a Complete RUT#

TypeScript
import { calculateVerifier, format } from 'rut.ts'
 
const body = '12345678'
const verifier = calculateVerifier(body)
const completeRut = format(body + verifier)
 
console.log(completeRut) // '12.345.678-5'

Safe Mode#

TypeScript
import { calculateVerifier } from 'rut.ts'
 
// Without safe mode (throws)
try {
  calculateVerifier('123') // Too short
} catch (error) {
  console.error(error)
}
 
// With safe mode (returns null)
const result = calculateVerifier('123', { throwOnError: false })
console.log(result) // null
 
// Useful for form validation
const handleBodyInput = (body: string) => {
  const verifier = calculateVerifier(body, { throwOnError: false })
  if (verifier) {
    console.log(`Complete RUT: ${body}-${verifier}`)
  } else {
    console.log('Invalid body length or format')
  }
}

Input Flexibility#

TypeScript
import { calculateVerifier } from 'rut.ts'
 
// Accepts standard body separators (dots/hyphens removed)
calculateVerifier('18.972.631') // '7'
calculateVerifier('18-972-631') // '7'
 
// Handles leading zeros
calculateVerifier('018972631') // '7' (zeros removed)
 
// Rejects letters and unsupported punctuation
calculateVerifier('18abc972631', { throwOnError: false }) // null

Validation Rules#

The function validates:

  • ✅ Body must be 7-8 digits after cleaning leading zeros
  • ✅ Only numeric characters allowed (no K in body)
  • ✅ Formatting characters (dots, hyphens, whitespace) are automatically removed
  • ✅ Letters and unsupported punctuation are rejected instead of silently stripped

Use Cases#

  • ✅ Generate custom RUTs with specific body numbers
  • ✅ Verify RUT correctness by recalculating verifier
  • ✅ Build RUT generation tools
  • ✅ Educational purposes (demonstrate Modulo 11)
  • ✅ Testing and debugging RUT validation

Understanding the Algorithm#

Example calculation for body 12345678:

Code
Step 1: Reverse digits and multiply
8×2 + 7×3 + 6×4 + 5×5 + 4×6 + 3×7 + 2×2 + 1×3
= 16 + 21 + 24 + 25 + 24 + 21 + 4 + 3
= 138

Step 2: Calculate modulo
138 % 11 = 6

Step 3: Get verifier
11 - 6 = 5 ✅