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:

  1. Remove all characters except digits and last char which can be digit or letter “K”
  2. Pad with zeros (“0″) string until it is exactly 9 symbols long
  3. 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)
  4. Find difference: 11 – (total % 11) (let’s call it rest)
  5. 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
  6. 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;
}
Be Sociable, Share!

14.7.2009

.Net,C#,Source Code

Dejan VesićComments (7)

7 Comments »

  1. 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

  2. Good to know for next time :)

    Comment by Dejan Vesić — 15.7.2009 @ 10:26:39

  3. […] 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

  4. Hej man, otkud ti da se bavis cileanskim rutom?! :-))

    Comment by Damir Colak — 10.5.2011 @ 02:32:18

  5. Posao ne bira :-)

    Comment by Dejan Vesić — 10.5.2011 @ 15:18:02

  6. This routine does not work and does not produce verifiable result.
    If you run RUT by it and then attempt to verify it against known RUT databases – it is hit or miss like 50/50 chances.
    Do not waste your time on integrating this one to your app.

    Comment by Alex — 5.2.2013 @ 21:55:11

  7. This is rather bold statement, especially 50/50 part.

    Do you have some examples (of not working well)?

    Regards,
    Dejan

    Comment by Dejan Vesić — 5.2.2013 @ 22:33:32

RSS feed for comments on this post. TrackBack URI

Leave a comment

*