WilvanBil.NationalRegisterNumber
2.3.0
dotnet add package WilvanBil.NationalRegisterNumber --version 2.3.0
NuGet\Install-Package WilvanBil.NationalRegisterNumber -Version 2.3.0
<PackageReference Include="WilvanBil.NationalRegisterNumber" Version="2.3.0" />
paket add WilvanBil.NationalRegisterNumber --version 2.3.0
#r "nuget: WilvanBil.NationalRegisterNumber, 2.3.0"
// Install WilvanBil.NationalRegisterNumber as a Cake Addin #addin nuget:?package=WilvanBil.NationalRegisterNumber&version=2.3.0 // Install WilvanBil.NationalRegisterNumber as a Cake Tool #tool nuget:?package=WilvanBil.NationalRegisterNumber&version=2.3.0
NationalRegisterNumber
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.
Features
- 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: Can format a
string
toYY.MM.DD-XXX.CC
format usingToFormattedNationalRegisterNumber()
string extension method. - Extraction: Can extract
DateOnly
birthdate andBiologicalSex
usingTryExtractBirthDate
andTryExtractBiologicalSex
string extension method.
Installation
Add the package to your project via the .NET CLI:
dotnet add package WilvanBil.NationalRegisterNumber
Alternatively, find it on NuGet Package Manager by searching for WilvanBil.NationalRegisterNumber
.
Usage
After installation, use the static class NationalRegisterNumberGenerator
to generate or validate national register numbers.
Validation
The IsValid
method checks whether a given national register number is valid and returns a boolean (true
/false
).
bool isValid = NationalRegisterNumberGenerator.IsValid("90022742191");
Generation
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);
Parameters
followNumber
: A number between1
and998
(inclusive). This parameter helps ensure uniqueness when generating numbers for the same date.minDate
must be greater than or equal to1900/01/01
.
Exceptions
If the input parameters are invalid, the following exceptions may be thrown:
ArgumentException
: Indicates an invalid parameter with an explanatory message.
Formatting
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
Notes
- 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
IsValid
method fromNationalRegisterNumberGenerator
for additional validation.
Extraction
You can extract DateOnly
or BiologicalSex
using TryExtractBiologicalSex
and TryExtractBirthDate
, however it will not check for full validity of the given input.
For this you can use the IsValid
method.
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.");
Example Usage
Validate a National Register Number
bool isValid = NationalRegisterNumberGenerator.IsValid("90022742191");
Console.WriteLine($"Is valid: {isValid}");
Generate and format a Random National Register Number
string randomNumber = NationalRegisterNumberGenerator.Generate();
Console.WriteLine($"Generated number: {randomNumber.ToFormattedNationalRegisterNumber()}");
Generate with Specific Parameters
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);
Extract birthdate
var nationalRegisterNumber = "90020200395";
if (nationalRegisterNumber.TryExtractBirthDate(out var birthDate))
Console.WriteLine($"Birth Date: {birthDate}");
else
Console.WriteLine("Invalid birth date.");
Contributing
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.
Issues
For any issues, requests or any other kind of feedback, please consider creating an issue.
Running Tests
Before submitting your changes, run the test suite:
dotnet test
!! WARNING
This 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.
License
This project is licensed under the MIT License. See the LICENSE
file for more details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.