Validate a Belgian national number in C# (with unit tests)

•4 min read
Image for Validate a Belgian national number in C# (with unit tests)

A Belgian national number validator can pass every checksum test and still accept a number that was never assigned. Name the boundary before you write the method.

The implementation below checks an eleven-digit representation and its modulo-97 checksum. It does not authenticate a person, query the National Register or implement every rule for dates and BIS numbers.

TL;DR

  • Accept eleven ASCII digits at this boundary.
  • Check both century-dependent checksum calculations.
  • Keep date and registration rules in a separate domain validator.
  • Use synthetic fixtures only in isolated test environments.

The checksum has two possible inputs

A Belgian national number has nine data digits followed by two check digits. For someone born before 2000, subtract the remainder of the nine-digit value divided by 97 from 97.

For someone born from 2000 onward, prefix the nine digits with 2 before calculating. Numerically, that adds 2_000_000_000. Use long: the prefixed value can exceed int.MaxValue.

The result ranges from 1 to 97. A zero remainder gives a checksum of 97, not 00.

Without a trusted full birth date, a checksum-only function can accept either calculation. If your application already has a verified date, a separate domain check should enforce the matching century rather than accepting both blindly.

Keep the method narrow

This code uses only the .NET base class library. Put it in a class library or your application’s validation layer.

public static class BelgianNationalNumber
{
    public static bool HasValidChecksum(string? value)
    {
        if (value is null || value.Length != 11)
        {
            return false;
        }

        long body = 0;
        int supplied = 0;

        for (int i = 0; i < value.Length; i++)
        {
            char character = value[i];
            if (character < '0' || character > '9')
            {
                return false;
            }

            int digit = character - '0';
            if (i < 9)
            {
                body = body * 10 + digit;
            }
            else
            {
                supplied = supplied * 10 + digit;
            }
        }

        return supplied == 97 - body % 97
            || supplied == 97 - (body + 2_000_000_000L) % 97;
    }
}

The ASCII check is deliberate. char.IsDigit accepts more than the characters 0 through 9. That is useful for some text processing, but identifiers exchanged with external systems need a defined representation.

This method rejects 90.02.01-997.04. If your UI accepts that display format, parse it at the UI boundary with an explicit format rule, then pass the eleven digits to this method. Do not remove every non-digit character: that can turn corrupted input into an apparently valid identifier.

Returning false is the validation contract. The caller should turn it into a clear validation message, rather than silently dropping the record.

Unit tests should cover the century rule and its limits

The following is an xUnit test class. Add a reference to the project containing the validator and use the namespace of that project if it has one.

using Xunit;

public sealed class BelgianNationalNumberTests
{
    [Theory]
    [InlineData("90020199704")] // Pre-2000 calculation.
    [InlineData("00010100105")] // Calculation with the 2 prefix.
    [InlineData("00000000097")] // Checksum-only: no real date.
    public void Accepts_matching_checksums(string value)
    {
        Assert.True(BelgianNationalNumber.HasValidChecksum(value));
    }

    [Theory]
    [InlineData(null)]
    [InlineData("")]
    [InlineData("9002019970")]
    [InlineData("900201997040")]
    [InlineData("90020199705")]
    [InlineData("00010100106")]
    [InlineData("00000000000")]
    [InlineData("90.02.01-997.04")]
    [InlineData("9002019970x")]
    [InlineData("90020199704")]
    public void Rejects_bad_shape_or_checksum(string? value)
    {
        Assert.False(BelgianNationalNumber.HasValidChecksum(value));
    }
}

Run the tests with dotnet test in your test project.

The all-zero body with checksum 97 is intentional. It proves that this function checks arithmetic rather than a real date or an assigned identity. A test that exposes the limit is more useful than a method called IsValid that hides it.

Full validation needs an agreed domain

An application that asks for an INSZ/NISS may need to support BIS numbers as well as National Register numbers. Registers also have rules for incompletely known dates. A calendar parser alone does not cover that domain.

Decide what the field accepts with the team that owns the integration. Then add focused checks for that contract. Keep authorization and identity verification outside the checksum helper.

Use the Belgian national number generator to prepare synthetic fixtures. Freeze the chosen fixtures in source control and keep them away from production services. A generated checksum-valid number can coincide with an assigned number.

The useful guarantee is small and precise: this string has the expected shape and passes one of the checksum calculations. Build the rest of the workflow around that guarantee.

Thank you for reading.

-Bruno

Author's note: I wrote the recipe and cooked the food, AI just helped with the plating.

Read more