Skip to content

Instantly share code, notes, and snippets.

@nilsmagnus
Last active September 13, 2021 13:25
Show Gist options
  • Save nilsmagnus/13973b2b6bfa8b2168180b8eb6119bcf to your computer and use it in GitHub Desktop.
Save nilsmagnus/13973b2b6bfa8b2168180b8eb6119bcf to your computer and use it in GitHub Desktop.
Validate norwegian organization number with javascript
// 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