Format and validate Chilean RUT (12.345.678-5) from Shopify

:bullseye: What is your goal?

I receive the RUT (Chilean tax ID) from customers via Shopify—they store it in the “ZIP” or “Company” field, as the checkout process doesn’t have a dedicated RUT field—and I need to send it to my invoicing system in the full Chilean format: 16.012.312-4.

:thinking: What is the problem & what have you tried?

In the (now closed) thread “Chilean RUT (DNI) Format,” the suggested solution added the hyphen but not the dots. I came up with this approach, in case it’s useful to anyone else:

Clean the value using a “Set variable” module (rut_limpio):
{{upper(replace(1.zip; /[^0-9kK]/g; emptystring))}}

Apply the format:
{{replace(substring(rut_limpio; 0; length(rut_limpio) - 1); /\B(?=(\d{3})+(?!\d))/g; “.”)}}-{{substring(rut_limpio; length(rut_limpio) - 1; length(rut_limpio))}}

Result: 160123124 → 16.012.312-4; this also works when the check digit is “K.”

What I’m still missing: validating the check digit (using the “modulo 11” algorithm) before sending the data, as customers frequently make typos when entering it. Has anyone managed to do this using only native Make functions—without relying on a code module or an external API? The tricky part is iterating through the digits and multiplying them by 2, 3, 4, 5, 6, and 7.


:bullseye: What is your goal?

I receive the RUT from customers via Shopify (they save it in the ZIP or Company field because the checkout doesn’t have a specific RUT field), and I need to send it to my billing system in the complete Chilean format: 16.012.312-4.

:thinking: What is the problem & what have you tried?

In the “Chilean RUT (DNI) Format” topic (now closed), the proposed solution added the hyphen but not the dots. I solved it this way, in case it’s useful to anyone else:

  1. Clean the value using a “Set variable” module (rut_limpio):

{{upper(replace(1.zip; /[^0-9kK]/g; emptystring))}}

  1. Apply formatting:

{{replace(substring(rut_limpio; 0; length(rut_limpio) - 1); /\B(?=(\d{3})+(?!\d))/g; “.”)}}-{{substring(rut_limpio; length(rut_limpio) - 1; length(rut_limpio))}}

Result: 160123124 → 16.012.312-4, and it works when the check digit is K.

What I’m still missing: validating the check digit (modulo 11) before sending it, since customers often make mistakes when typing it. Has anyone done this using only native Make functions—without using a code module or an external API? The tricky part is iterating through the digits and multiplying them by 2, 3, 4, 5, 6, and 7.

Hey there,

you can use slice function to get the individual digits, then use the math functions, or the math module for the operation.

It will be a mess if you use the functions directly, I suggest using the dedicated modules instead, they dont consume credits and are easier to manage.

Stoyan above has the right instinct — slice the digits and do the maths. Here’s the version of that which stays readable, and which answers your “no code module, no external API” constraint: stop trying to loop.

The weights only depend on a digit’s distance from the right, so if you left-pad the body to a fixed width, the weights stop moving and become constants.

Pad to 8 digits and the left-to-right weights are fixed: 3, 2, 7, 6, 5, 4, 3, 2. Then it’s eight substring calls, no module, no iteration.

Keeping your rut_limpio:

Body and check digit

body   = {{substring(rut_limpio; 0; length(rut_limpio) - 1)}}
dv_in  = {{substring(rut_limpio; length(rut_limpio) - 1; length(rut_limpio))}}

Left-pad without a leftpad function (slice a zero string — shorter RUTs shift right, and because the weights are right-anchored the result is unchanged):

b8 = {{substring("00000000"; 0; 8 - length(body))}}{{body}}

Weighted sum

{{3 * parseNumber(substring(b8;0;1)) + 2 * parseNumber(substring(b8;1;1)) + 7 * parseNumber(substring(b8;2;1)) + 6 * parseNumber(substring(b8;3;1)) + 5 * parseNumber(substring(b8;4;1)) + 4 * parseNumber(substring(b8;5;1)) + 3 * parseNumber(substring(b8;6;1)) + 2 * parseNumber(substring(b8;7;1))}}

Then the mapping (the easy half): d = 11 - (sum mod 11). If d = 11 the DV is 0; if d = 10 it’s K; otherwise it’s d itself. Compare that against dv_in in a filter and stop the scenario when they differ.

Verification I’d trust before wiring the filter: 11.111.111 → sum 32 → 32 mod 11 = 10 → 11 − 10 = 1, and 11.111.111-1 is valid. If your ICL build doesn’t expose mod, sum - 11 * floor(sum / 11) gives the same remainder.

Three guards worth adding, because they will bite you in production:

  • length(body) > 8 — reject before padding, otherwise substring silently returns garbage and you’ll validate nonsense.
  • Empty field — if rut_limpio is empty, dv_in and the computed DV both come out empty and the comparison passes, so add length(rut_limpio) >= 2 to the filter.
  • Your source is a shared field. Customers writing a RUT into ZIP or Company will sometimes paste a phone number or a company name that survives the [^0-9kK] strip. A passing mod-11 check on a random 8-digit number is not proof it’s a RUT — for invoicing you probably want a second condition (starts with a plausible prefix, or a manual review branch) before it goes downstream.

The 8-substring version stays readable up to ~10 digits. Past that, don’t fight it — a Code module or a lookup table is the honest answer, even though it’s not what you asked for.

The check digit is modulo 11. Take the digits from the right, multiply them by 2 to 7 repeating, add them up, then do 11 minus (sum mod 11). An 11 means 0 and a 10 means K. In Make that’s easiest with an iterator and aggregator over the digits, then compare the result to the last character.