Skip to content

The all in one library for swedish identification numbers such as personal (personnummer), coordination (samordningsnummer) and business registration number (organisationsnummer).

License

Notifications You must be signed in to change notification settings

psafth/swedish-identification-number

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

74 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

swedish-identification-number

The all in one library for swedish identification numbers such as personal (personnummer), coordination (samordningsnummer) and business registration number (organisationsnummer).

test dotnet-status event parameter codeql-status event parameter

Usage

Personal identification number (personnummer)

// Given an string input such as 197607012395 or 7607012395... or 760701-2395.. or 19760701-2395
string strVal = "197607012395";                     

// Use the extension methods ToIdentificationNumber to parse it as an identification number. Badly formatted strings will throw an exception.
PersonIdentificationNumber identificationNumber = strVal.ToIdentificationNumber();

// Check if the number is valid according to the Luhn algorithm.
bool isValid = identificationNumber.IsValid;        

// Get the persons gender (sex)
var gender = identificationNumber.Gender;

// Geth the persons date of birth
var birth = identificationNumber.DateOfBirth;

Console.WriteLine(identificationNumber);                    // Output: 197607012395
Console.WriteLine(identificationNumber.ToFormalString());   // Output: 760701-2395

Coordination number (samordningsnummer)

string strVal = "5401642383";

PersonIdentificationNumber identificationNumber = strVal.ToIdentificationNumber();

bool isValid = identificationNumber.IsValid;

Console.WriteLine(identificationNumber);                                        // Output: 195401642383
Console.WriteLine(identificationNumber.ToFormalString());                       // Output: 540164-2383
Console.WriteLine(identificationNumber.DateOfBirth.ToString("yyyy-MM-dd"));     // Output: 1954-01-04

Business registration number (orgisationsnummer)

string strVal = "2120000142";             // or 212000-0142

BusinessRegistrationNumber identificationNumber = strVal.ToIdentificationNumber();

Console.WriteLine(identificationNumber);                                        // Output: 2120000142
Console.WriteLine(identificationNumber.ToFormalString());                       // Output: 212000-0142
Console.WriteLine(identificationNumber.BusinessForm);                           // Output: GovernmentAgency

Validation attributes

GitHub issue/pull request detail

Fluid validation

GitHub issue/pull request detail

Ready for Entity Framework

E.g. A simple customer entity where a customer can be either a private customer or a business customer.

The property IdentificationNumber can hold any type of identification number implementing the IIdentificationNumber interface. There is no need for any backing field or multiple properties.

public class Customer
{
    [Key]
    public Guid Id { get; set; }
    
    public IIdentificationNumber IdentificationNumber { get; set; }
    
    public CustomerType Type { get; set; }
    public string Name { get; set; }
}

With value conversion in EF:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder
        .Entity<Customer>()
        .Property(e => e.IdentificationNumber)
        .HasConversion(
            v => v.ToString(),
            v => v.ToIdentificationNumber());
}

About

The all in one library for swedish identification numbers such as personal (personnummer), coordination (samordningsnummer) and business registration number (organisationsnummer).

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages