Verifying Chilean RUT code (tax number) in C#
In one of the projects at my work, I got task to verify some data (in migration process from old system to new one); one of the fields was Chilean RUT (Rol Único Tributario) or Tax Identification Number.
The RUT has 8 digits plus a validation number or letter (xx.xxx.xxx-z) and algorithm for verification (as per my knowledge) is as follows:
- Remove all characters except digits and last char which can be digit or letter “K”
- Pad with zeros (“0″) string until it is exactly 9 symbols long
- Multiply, from left to right, first 8 digits with following factors: 3, 2, 7, 6, 5, 4, 3, 2 and sum those results (let’s call it total)
- Find difference: 11 – (total % 11) (let’s call it rest)
- Determine control char by:
-
5.1 If rest = 11, than control char “0” (zero)
5.2 If rest = 10, than control char “K”
5.3 Otherwise, rest is control char - If the original control char (9th char) is equal to calculated control char, RUT is valid.
C# code for this could be like this:
/// <summary> /// Routine for checking of RUT correctness /// </summary> /// <param name="rut">RUT to check</param> /// <returns>true if RUT is valid</returns> /// <remarks>Only numbers and optional "K" at the end of string are expected /// </remarks> public static bool IsRutOk(string rut) { const string RutRegex = "[0-9]+K?"; Regex RegExRut = new Regex(RutRegex, RegexOptions.Compiled | RegexOptions.IgnoreCase); int[] coefs = {3, 2, 7, 6, 5, 4, 3, 2}; // In case that rut is padded with spaces rut = rut.Trim().ToUpperInvariant(); if (!RegExRut.IsMatch(rut)) { return false; } if (rut.Length > 9) { return false; } // If shorter than 9 characters (8 + control char) ... while (rut.Length < 9) { rut = "0" + rut; } int total = 0; for (int index = 0; index < rut.Length - 1; index++) { char curr = rut.Substring(index, 1).ToCharArray()[0]; total += coefs[index]*(curr - '0'); } int rest = 11 - (total%11); if (rest == 11) rest = 0; if ((rest == 10) && rut.EndsWith("K")) { return true; } if (rut.Substring(rut.Length - 1, 1).ToCharArray()[0] == ('0' + rest)) { return true; } return false; }
14.7.2009

You could ask me and Deki for the function or you could have written it earlier. At least one of us would not have to do it then
Comment by Aleksandar — 15.7.2009 @ 09:45:58
Good to know for next time
Comment by Dejan Vesić — 15.7.2009 @ 10:26:39
[...] errors. If you’re interested in the validation algorithm itself, read the pseudocode on this blog post.No RUT, no service?If you’re a visitor to Chile, you’ll discover quickly– perhaps [...]
Pingback by The RUT « expat.cl — 4.8.2009 @ 20:25:17