Last active
February 22, 2024 12:07
-
-
Save devbyray/cc491f00a2f7630b323e80c0df828d3a to your computer and use it in GitHub Desktop.
How to validate BSN, PGN & OWN in the Netherlands with TypeScript/JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function elfProefValidation(value, type) { | |
let returnValue = false; | |
if (!value || value?.length === 0) { | |
return true; | |
} | |
if (value === '00000000000' || value.length !== 9) { | |
return false; | |
} | |
const values = value.split(''); | |
const firstCharacter = parseInt(values[0], 10); | |
const lastCharacter = parseInt(values[values.length - 1], 10); | |
const [a, b, c, d, e, f, g, h, i] = values.map((char) => parseInt(char, 10)); | |
let result = 0; | |
if (type === 'bsn') { | |
result = 9 * a + 8 * b + 7 * c + 6 * d + 5 * e + 4 * f + 3 * g + 2 * h + -1 * i; | |
returnValue = result > 0 && result % 11 === 0; | |
} else if (type === 'own') { | |
result = 9 * a + 8 * b + 7 * c + 6 * d + 5 * e + 4 * f + 3 * g + 2 * h; | |
returnValue = result > 0 && firstCharacter === 1 && result % 11 === lastCharacter + 5; | |
} else { | |
returnValue = false; | |
} | |
return returnValue; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export enum ElfproefType { | |
bsn, | |
own | |
} | |
function elfProefValidation(value: string, type: ElfproefType): boolean { | |
let returnValue = false; | |
if (!value || value?.length === 0) { | |
return true; | |
} | |
if (value === '00000000000' || value.length !== 9) { | |
return false; | |
} | |
const values = value.split(''); | |
const firstCharacter = parseInt(values[0], 10); | |
const lastCharacter = parseInt(values[values.length - 1], 10); | |
const [a, b, c, d, e, f, g, h, i] = values.map((char: string) => parseInt(char, 10)); | |
let result = 0; | |
if (type === ElfproefType.bsn) { | |
result = 9 * a + 8 * b + 7 * c + 6 * d + 5 * e + 4 * f + 3 * g + 2 * h + -1 * i; | |
returnValue = result > 0 && result % 11 === 0; | |
} else if (type === ElfproefType.own) { | |
result = 9 * a + 8 * b + 7 * c + 6 * d + 5 * e + 4 * f + 3 * g + 2 * h; | |
returnValue = result > 0 && firstCharacter === 1 && result % 11 === lastCharacter + 5; | |
} else { | |
returnValue = false; | |
} | |
return returnValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to validate BSN, PGN & OWN in the Netherlands with TypeScript/JavaScript
In the Netherlands we have a few identification numbers from the government. In this example you can I created a function to validate both of them based on the specification.
How to use the code?
Define the function in your code and call it like this:
TypeScript example
JavaScript example