Validates Saudi identification numbers — National ID, Iqama and Border numbers.
Every result carries a stable code (the ID type when valid, an error code when invalid) plus a default English message, so you can show your own localized text in any language.
A number is valid when it:
- is exactly 10 digits,
- starts with
1,2,3or4, - passes the Luhn checksum.
Input is trimmed and Arabic-Indic digits (٠-٩, ۰-۹) are converted to ASCII digits before validating.
Targets netstandard2.0 and net8.0, so it works on .NET Framework 4.6.1+, .NET Core, and .NET 5+.
dotnet add package SaudiIdValidatorusing SaudiIdValidator;
// Quick check
bool ok = SaudiId.IsValid("1087654321");
// Full result
SaudiIdResult result = SaudiId.Validate("1087654321");
Console.WriteLine(result.IsValid); // True
Console.WriteLine(result.Code); // NationalId
Console.WriteLine(result.Message); // National ID
// Numeric input is supported too
SaudiId.IsValid(1087654321L);| Code | Value | Valid | Default message |
|---|---|---|---|
NationalId |
1 | ✔ | National ID |
Iqama |
2 | ✔ | Iqama ID |
BorderNumber |
3 | ✔ | Border Number (IDs starting 3 or 4) |
InvalidFormat |
101 | ✘ | ID should be 10 digits long |
InvalidPrefix |
102 | ✘ | ID should start with 1, 2, 3, or 4 |
InvalidChecksum |
103 | ✘ | ID failed Luhn algorithm |
Values below 100 are valid ID types, and values of 100 and above are errors. The numeric values are stable and will not change, so it's safe to store them or use them as keys.
Map result.Code to your own text, for example with a .resx resource file or a dictionary:
var arabic = new Dictionary<SaudiIdCode, string>
{
[SaudiIdCode.NationalId] = "هوية وطنية",
[SaudiIdCode.Iqama] = "بطاقة إقامة",
[SaudiIdCode.BorderNumber] = "رقم حدود",
[SaudiIdCode.InvalidFormat] = "رقم الهوية يجب أن يتكون من 10 أرقام",
[SaudiIdCode.InvalidPrefix] = "رقم الهوية يجب أن يبدأ بـ 1، 2، 3، أو 4",
[SaudiIdCode.InvalidChecksum] = "رقم الهوية غير صحيح",
};
var result = SaudiId.Validate(input);
string text = arabic.TryGetValue(result.Code, out var t) ? t : result.Message;SaudiId.GetDefaultMessage(code) returns the English default for any code.
| Property | Type | Description |
|---|---|---|
IsValid |
bool |
true when all checks pass |
Id |
string |
The normalized ID that was validated |
Code |
SaudiIdCode |
ID type when valid, error code when invalid |
Message |
string |
Default English message (type name or failure reason) |
The repository includes a small console app:
# validate arguments (exit code 0 if all valid, 1 otherwise)
dotnet run --project src/SaudiIdValidator.ConsoleApp -- 1087654321 2345678904
# interactive mode
dotnet run --project src/SaudiIdValidator.ConsoleAppsrc/SaudiIdValidator/ Core library (the NuGet package)
src/SaudiIdValidator.ConsoleApp/ Console app for trying the validator
tests/SaudiIdValidator.Tests/ xUnit tests
Requires the .NET 10 SDK.
dotnet build
dotnet test
dotnet pack src/SaudiIdValidator -c Release -o artifactsPushing a tag like v1.0.0 triggers the GitHub Actions workflow in .github/workflows/publish.yml, which tests, packs and pushes the package to NuGet using Trusted Publishing, so no long-lived API key is stored. It requires a Trusted Publishing policy on nuget.org for this repository and workflow file (publish.yml), and a repository secret NUGET_USER containing the nuget.org username. The tag version overrides the <Version> in the csproj.
Issues and pull requests are welcome at github.com/aalgrou/SaudiId-validator. Please add or update tests for any behavior change.
Abdullah AlGrou — @aalgrou
MIT © Abdullah AlGrou