How to validate BSN, PGN & OWN in the Netherlands with TypeScript/JavaScript
Tags: javascript, typescript, coding, webdev
Published: 2022-01-27 09:03:23

---
title: How to validate BSN, PGN & OWN in the Netherlands with TypeScript/JavaScript
published: true
date: 2022-01-19 14:45:13 UTC
tags: javascript, typescript, coding, webdev
canonical_url: https://hasnode.byrayray.dev/how-to-validate-bsn-pgn-and-own-in-the-netherlands-with-typescriptjavascript
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qbwldplg61nahiggfxiz.jpg
---

The BSN, PGN and OWN in the Netherlands can be validated with a 11 proef (elf proef). In this post I show you the JavaScript and TypeScript code.

In the Netherlands, we have a few identification numbers from the government. In this example, you can create a function to validate both of them [based on the specification](https://developers.wiki.kennisnet.nl/index.php?title=OSO:2022/beveiliging/versleuteling_bsn).

- BSN = Burgerservice nummer
- PGN = Burgerservice nummer in schools where the student doesn't have a BSN. The PGN is also called OWN.

![divider-byrayray.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629890886208/NhHYvPmBA.png)

## How to use the code?

Define the function in your code and call it like this:

### TypeScript example

```typescript
// PGN/OWN
elfProefValidation('114435231', ElfproefType.own); // returns true;
elfProefValidation('34564366349', ElfproefType.own); // returns false;
// BSN
elfProefValidation('232262536', ElfproefType.bsn); // returns true;
elfProefValidation('247594057', ElfproefType.bsn); // returns false;
```

### JavaScript example

```javascript
// PGN/OWN
elfProefValidation('114435231', 'own'); // returns true;
elfProefValidation('34564366349', 'own')); // returns false;
// BSN
elfProefValidation('232262536', 'bsn')); // returns true;
elfProefValidation('247594057', 'bsn')); // returns false;

```

### Working version

I created a working version on [RunKit](https://runkit.com/devbyrayray/elfproef) . _Make sure you select Node version 17._

{% runkit %}
function elfProefValidation(value, type) {
        let returnValue = false;
        if (!value || value.length === 0) {
            // This is to prevent behaviour that looks like the OWN is required
            return true;
        }
        if (value === '00000000000' || value.length !== 9) {
            return false;
        }
        const values = value.split('');
        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 && result % 11 === lastCharacter + 5;
        } else {
            returnValue = false;
        }

        return returnValue;
    }

const bsn = '010464554';
const own = '101211151';

console.log('BSN: ', elfProefValidation(bsn, 'bsn'));
console.log('OWN: ', elfProefValidation(own, 'own'));

{% endrunkit %}

## Code

{% gist https://gist.github.com/devbyray/cc491f00a2f7630b323e80c0df828d3a %}

![divider-byrayray.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629890886208/NhHYvPmBA.png)

## Thanks!

![hashnode-footer.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629789655319/nBF6anHH4w.png)_I hope you learned something new or are inspired to create something new after reading this story! 🤗 If so, consider subscribing via email (scroll to the top of this page) or follow me here on Hashnode._

> Did you know that you can create a [Developer blog like this one, yourself](https://hashnode.com/@devbyrayray/joinme)? It's entirely for free. 👍💰🎉🥳🔥

_If I left you with questions or something to say as a response, scroll down and type me a message. Please send me a [DM on Twitter @DevByRayRay](https://twitter.com/@devbyrayray) when you want to keep it private. My DM's are always open 😁_