NationalRegisterNumber is a .NET package for generating and validating Belgian national register numbers. The logic is based on the Official Documentation by the Belgian Government.
- Validation: Verify if a given national register number is valid.
- Generation: Generate valid national register numbers with flexible parameters such as birth date and biological sex.
- Formatting: Format a
stringtoYY.MM.DD-XXX.CCformat usingToFormattedNationalRegisterNumber()string extension method. - Extraction: Extract
DateOnlybirthdate andBiologicalSexusingTryExtractBirthDateandTryExtractBiologicalSexstring extension methods.
Add the package to your project via the .NET CLI:
dotnet add package WilvanBil.NationalRegisterNumberAlternatively, find it on NuGet Package Manager by searching for WilvanBil.NationalRegisterNumber.
After installation, use the static class NationalRegisterNumberGenerator to generate or validate national register numbers.
The IsValid method checks whether a given national register number is valid and returns a boolean (true/false).
bool isValid = NationalRegisterNumberGenerator.IsValid("90022742191");The Generate method creates valid national register numbers. The following overloads are available:
string number = NationalRegisterNumberGenerator.Generate();
string number = NationalRegisterNumberGenerator.Generate(DateOnly birthDate);
string number = NationalRegisterNumberGenerator.Generate(BiologicalSex sex);
string number = NationalRegisterNumberGenerator.Generate(DateOnly birthDate, BiologicalSex sex);
string number = NationalRegisterNumberGenerator.Generate(DateOnly minDate, DateOnly maxDate);
string number = NationalRegisterNumberGenerator.Generate(DateOnly minDate, DateOnly maxDate, BiologicalSex sex);
string number = NationalRegisterNumberGenerator.Generate(DateOnly birthDate, int followNumber);followNumber: A number between1and998(inclusive). This parameter helps ensure uniqueness when generating numbers for the same date.minDate: Must be greater than or equal to1900-01-01.
If the input parameters are invalid, the following exceptions may be thrown:
ArgumentException: Indicates an invalid parameter with an explanatory message.
You can format Belgian National Register Numbers into the official format (YY.MM.DD-XXX.CC) using the ToFormattedNationalRegisterNumber string extension method.
string nationalRegisterNumber = "90020200395";
string formattedNumber = nationalRegisterNumber.ToFormattedNationalRegisterNumber();
Console.WriteLine(formattedNumber); // Output: 90.02.02-003.95- Input Validation: The method will return the original string if:
- The input is null, empty, or not exactly 11 characters.
- You can combine this with the
IsValidmethod fromNationalRegisterNumberGeneratorfor additional validation.
You can extract DateOnly or BiologicalSex using TryExtractBiologicalSex and TryExtractBirthDate. Note that these methods do not check for full validity of the given input.
For full validation, use the IsValid method first.
var nationalRegisterNumber = "90020200395";
if (!NationalRegisterNumberGenerator.IsValid(nationalRegisterNumber))
return;
if (nationalRegisterNumber.TryExtractBiologicalSex(out var sex))
Console.WriteLine($"Biological Sex: {sex}");
else
Console.WriteLine("Invalid biological sex.");
if (nationalRegisterNumber.TryExtractBirthDate(out var birthDate))
Console.WriteLine($"Birth Date: {birthDate}");
else
Console.WriteLine("Invalid birth date.");bool isValid = NationalRegisterNumberGenerator.IsValid("90022742191");
Console.WriteLine($"Is valid: {isValid}");string randomNumber = NationalRegisterNumberGenerator.Generate();
Console.WriteLine($"Generated number: {randomNumber.ToFormattedNationalRegisterNumber()}");string numberByDate = NationalRegisterNumberGenerator.Generate(new DateOnly(1990, 1, 1));
string numberBySex = NationalRegisterNumberGenerator.Generate(BiologicalSex.Male);
string numberByDateAndSex = NationalRegisterNumberGenerator.Generate(new DateOnly(1990, 1, 1), BiologicalSex.Female);var nationalRegisterNumber = "90020200395";
if (nationalRegisterNumber.TryExtractBirthDate(out var birthDate))
Console.WriteLine($"Birth Date: {birthDate}");
else
Console.WriteLine("Invalid birth date.");We welcome contributions to this package! To get started:
-
Fork the repository: WilvanBil/NationalRegisterNumber.
-
Clone your fork:
git clone https://github.com/YourUsername/NationalRegisterNumber.git
-
Create a new branch for your feature or bug fix:
git checkout -b feature/my-new-feature
-
Make your changes and ensure all tests pass.
-
Submit a pull request with a clear description of your changes.
For any issues, requests, or feedback, please consider creating an issue.
Before submitting your changes, run the test suite:
dotnet testThis package is intended for testing and research purposes only. Do not use it in a live or production environment. It is specifically designed for unit and integration testing scenarios.
This project is licensed under the MIT License. See the LICENSE file for more details.
