Last active
September 13, 2021 13:25
-
-
Save nilsmagnus/13973b2b6bfa8b2168180b8eb6119bcf to your computer and use it in GitHub Desktop.
Validate norwegian organization number with 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
// validate org number according to https://www.brreg.no/om-oss/oppgavene-vare/alle-registrene-vare/om-enhetsregisteret/organisasjonsnummeret/ | |
function validateNorwegianOrgNumber(rawNumber: string): boolean { | |
if ( | |
rawNumber == null || | |
rawNumber.trim().length !== 9 || | |
!/^\d*$/.test(rawNumber.trim()) | |
) | |
return false; | |
const numbers = rawNumber.trim(); | |
const controlNumbers = [3, 2, 7, 6, 5, 4, 3, 2]; | |
const sum = rawNumber | |
.substr(0, 8) | |
.split("") | |
.map((v, i) => parseInt(v, 10) * controlNumbers[i]) | |
.reduce((acc, value) => acc + value ); | |
return 11 - (sum % 11) === parseInt(numbers.charAt(8), 10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment