calculateVerifier()
Calculates the verifier digit for a given RUT body using the Modulo 11 algorithm.
Basic Usage
import { calculateVerifier } from 'rut.ts'
calculateVerifier('12345678') // '5'
calculateVerifier('24657622') // 'K'Type Signature
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 optionsthrowOnError(boolean, default:true) - If false, returnsnullinstead of throwing
Return Value
Returns VerifierDigit or null:
- A single character:
'0'to'9'or'K' nullif invalid (only in safe mode)
Algorithm
The function implements the Modulo 11 algorithm:
- Take the RUT body digits
- Multiply each digit by factors (2,3,4,5,6,7,2,3,4…) from right to left
- Sum all products
- Calculate
11 - (sum % 11) - If result is 11, verifier is
'0' - If result is 10, verifier is
'K' - Otherwise, verifier is the result as a string
Examples
Basic Calculation
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
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
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
import { calculateVerifier } from 'rut.ts'
// Accepts formatted input (dots/hyphens removed)
calculateVerifier('18.972.631') // '7'
calculateVerifier('18-972-631') // '7'
// Handles leading zeros
calculateVerifier('018972631') // '7' (zeros removed)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) are automatically removed
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:
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 ✅Related Functions
validate()- Validate complete RUTgenerate()- Generate random valid RUTsdecompose()- Extract body and verifier
Last updated on