PowerShell: Validating RRNr

Problem

I want to validate any given RRNr (Rijksregisternummer, the Belgian equivalent of a SSN).

Solution

Steps

I looked up the rules for a valid RRNr

  • Has 11 digits divided in 3 parts of 6, 3, and 2 digits
  • The first part equals the date of birth formatted yymmdd
  • The third part is a checksum for the first 9 digits
  • If the year of birth is 2000 or later an extra 2 must be prepended to the 9 digits before calculating the checksum
  • The checksum equals 97 deducted by modulus 97 of the 9 or 10 digit number

Then I wrote a cmdlet that could validate a RRNr if the DateOfBirth is also given.

Function Get-RRNrValidation {
	<#
	.SYNOPSIS
		The Get-RRNrValidation cmdlet validates the RRNr (Rijksregisternummer, the Belgian equivalent of a SSN)

	.DESCRIPTION
		The Get-RRNrValidation cmdlet will validates the RRNr (Rijksregisternummer, the Belgian equivalent of a SSN) against the folowing rules:
			- Format = xxxxxx-xxx-xx (all digits)
			- First part of 6 digits equals the date of birth formated yymmdd
			- Third part is a checksum for the first 9 digits
                        - If the year of birth is 2000 or later an extra 2 must be prepended to the 9 digits before calculating the checksum
                        - The checksum equals 97 deducted by modulus 97 of the 9 or 10 digit number
	
	.PARAMETER RRNr
		RRNr to be validated
	
	.PARAMETER DateOfBirth
		DateOfBirth of the person who's RRNr needs to be validated
	
	.EXAMPLE
		Get-RRNrValidation "991231-123-58" (Get-Date -Year 1999 -Month 12 -Day 31)
	
	.EXAMPLE
		Get-RRNrValidation -RRNr "991231-123-58" -DateOfBirth (Get-Date -Year 1999 -Month 12 -Day 31)

	.INPUTS
		System.String, System.DateTime
	
	.OUTPUTS
		System.Boolean
	#>
	[CmdletBinding()]
	Param(
		[Parameter(
			Mandatory = $True,
			Position = 1,
			ValueFromPipeline = $False,
			ValueFromPipelineByPropertyName = $True,
			HelpMessage="Enter a valid RRNR xxxxxx-xxx-xx"
		)]
		[ValidatePattern("\d{6}-\d{3}-\d{2}")]
		[string] $RRNr,
		[Parameter(
			Mandatory = $True,
			Position = 2,
			ValueFromPipeline = $False,
			ValueFromPipelineByPropertyName = $True
		)] [DateTime] $DateOfBirth
 	)
	$boolIsValid = $False
 	# Validate RRNr
  	$RRNrSplit = $RRNr.Split('-')
  	if([String]::Format("{0:yymmdd}",$DateOfBirth) -eq $RRNrSplit[0]){
     		Write-Verbose "Invalid RRNr and DateOfBirth do not match"
  	} else {
		$RRNrPartA = $RRNrSplit[0] + $RRNrSplit[1]
		$RRNrPartB = $RRNrSplit[2]
		# If DateOfBirth is 2000 or later add a 2 to the front of the number
		if($DateOfBirth.Year -ge 2000) {
			$RRNrPartA = "2" + $RRNrPartA
		}
		$intA = $Null
		$intB = $Null
		# If PartA and PartB can be parsed into int continue validation
		if([int32]::TryParse(($RRNrSplit[0] + $RRNrSplit[1]),[ref]$intA) -and [int32]::TryParse($RRNrSplit[2],[ref]$intB)){
			# If PartB equals 97 deducted by the modulus of PartB the RRNr is valid
			if($intB -eq (97 - ($intA % 97))){
				$boolIsValid = $True
				Write-Verbose "Valid RRNr"
			} else {
				Write-Verbose "Invalid RRNr"
			}
	    } else {
	        Write-Verbose "Invalid RRNr: must only contain numbers"
	    }
	}
	Write-Output $boolIsValid
}

Result

I put the following code into my other script

# Validate RRNr
If(-not (Get-RRNrValidation $RRNr $DateOfBirth)){
	throw "Invalid RRNr and/or DateOfBirth"
}
Advertisements

Tags: , , , , , , , ,

2 responses to “PowerShell: Validating RRNr”

  1. Ubinho says :

    Je bent vergeten dat er een check is met het geslacht. De middelste drie cijfers houden rekening met het geslacht indien oneven is het geslacht man. Indien de middelste drie cijfers even is dan is het geslacht vrouw.

    • Tomas Deceuninck says :

      Dat wist ik niet, is zeker handig voor in de toekomst.
      Op de plaats waar ik deze cmdlet gebruik beschik ik niet over het geslacht, dus ik kan de extra controle niet uitvoeren. Mensen die dit in hun script willen gebruiken en het geslacht wel als input hebben kunnen dit cmdlet zeker uitbreiden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: