Onderstaande functie kan gebruikt worden om Belgische rijksregisternummers te valideren.

De samenstelling van een rijksregisternummer wordt op deze wiki-pagina uitgelegd: http://nl.wikipedia.org/wiki/Rijksregisternummer

De code geeft een string terug. Deze bevat “M” of “F” naargelang het om een geldig mannelijk of vrouwelijk rijksregisternummer gaat, of een omschrijving van gevonden fouten indien ongeldig.

De functie laat als input ook spaties, punten (.) en dashes (-) toe om een nummer met deze karakters ook te kunnen controleren.

De namespaces System.Text.RegularExpressions en System.Globalization dienen gerefenceerd te worden.


using System.Text.RegularExpressions;
using System.Globalization;

public string CheckRRNumberValidity(string input)
{
    if (input != null)
    {
        string message = "";

        Regex nonAllowedCharacters = new Regex(@"[^0-9. -]");
        Regex nonNumeric = new Regex(@"[^0-9]");

        string rrnumber = input;

        if (nonAllowedCharacters.IsMatch(rrnumber))
            return "Non allowed character";
        else
        {
            //STRIP THE NUMBER FROM ALL NON NUMERIC CHARS
            rrnumber = nonNumeric.Replace(rrnumber, "");

            //LENGTH MUST BE 11 DIGITS
            if (rrnumber.Length != 11)
                return "Length != 11";
            else
            {

                try
                {
                    bool birthDateOK = false;
                    bool counterOK = false;
                    bool controlOK = false;

                    bool born2kOrLater = false;

                    string gender = "(unknown)";

                    //FIRST 6 DIGITS ARE BIRTHDATE IN FORMAT YYMMDD
                    string birthDatePart = rrnumber.Substring(0, 6);

                    //NEXT 3 ARE COUNTER
                    string counterPart = rrnumber.Substring(6, 3);

                    //LAST 2 ARE CONTROLNUMBER
                    string controlPart = rrnumber.Substring(9, 2);

                    /* 1. CONTROL NUMBER CHECKING */
                    /******************************/

                    //CALCULATE CONTROLNUMER (= MOD 97 OF FIRST 9 DIGITS)
                    int calculatedControl = 97 - (int)(Int64.Parse(birthDatePart + counterPart) % 97);

                    if (calculatedControl != Int32.Parse(controlPart))
                    {
                        /* IF THE CALCULATED CONTROL PART IS DIFFERENT THAN THE ONE IN THE INPUTSTRING
                         * ADD A "2" IN FRONT OF THE BIRTHDATEPART AND RECALCULATE. THIS WAS INTRODUCED TO
                         * ALLOW BIRTHDATES OF YEAR 2000 AND LATER
                        */

                        calculatedControl = 97 - (int)(Int64.Parse("2" + birthDatePart + counterPart) % 97);

                        if (calculatedControl != Int32.Parse(controlPart))
                        {
                            /* THE CALCULATION STILL DOESN'T MATCH THE CONTROLNUMER, SO THIS IS AN INVALID
                             * REGISTRY NUMBER
                            */

                            controlOK = false;
                        }
                        else
                        {
                            born2kOrLater = true;
                            controlOK = true;
                        }
                    }
                    else
                        controlOK = true;

                    /* 2. BIRTHDATE CHECKING */
                    /*************************/

                    string d = birthDatePart;
                    //BUILD THE BIRTHDATE TO CHECK
                    if (born2kOrLater == true)
                        d = "20" + birthDatePart;
                    else
                        d = "19" + birthDatePart;
                    //END BUILD

                    string format = "yyyyMMdd";
                    DateTime birthDate;

                    birthDateOK = DateTime.TryParseExact(d, format, CultureInfo.CurrentCulture, DateTimeStyles.None, out birthDate);
                    Console.WriteLine(birthDate.ToString("dd/MM/yyyy"));

                    /* 3. COUNTER CHECKING */
                    /***********************/

                    /* COUNTERPART MUST BE BETWEEN 001 AND 997
                     * EVEN FOR FEMALE
                     * ODD FOR MALE
                    */
                    int counter = Int32.Parse(counterPart);

                    if (counter < 1 || counter > 997)
                        counterOK = false;
                    else if (counter % 2 == 0) //EVEN
                    {
                        counterOK = true;
                        gender = "F"; //FEMALE
                    }
                    else
                    {
                        counterOK = true;
                        gender = "M"; //MALE
                    }

                    /* 4. RETURN GENDER OR MESSAGE */
                    /*******************************/

                    if (!birthDateOK)
                        message += "Invalid birthdate;";
                    if (!counterOK)
                        message += "Invalid counter;";
                    if (!controlOK)
                        message += "Invalid control number;";

                    if (!message.Equals("")) //THERE ARE ERRORS
                    {
                        if (message.EndsWith(";"))
                            message = message.Substring(0, message.Length - 1);

                        return message;
                    }
                    else //IF GETTING HERE, NUMBER SHOULD BE CORRECT AND GENDER FILLED IN
                        return gender;
                }
                catch (Exception ex)
                {
                    return "Exception";
                }
            }
        }
    }
    else
    {
        return "isnull";
    }
}