Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion stdnum/no/orgnr.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,25 @@
"""Orgnr (Organisasjonsnummer, Norwegian organisation number).

The Organisasjonsnummer is a 9-digit number with a straightforward check
mechanism.
mechanism. The first digit is 8 or 9 and the last digit is a check digit
calculated using modulus 11.

More information:

* https://nn.wikipedia.org/wiki/Organisasjonsnummer
* https://no.wikipedia.org/wiki/Organisasjonsnummer
* https://www.brreg.no/om-oss/registrene-vare/om-enhetsregisteret/organisasjonsnummeret/

>>> validate('988 077 917')
'988077917'
>>> validate('988 077 918')
Traceback (most recent call last):
...
InvalidChecksum: ...
>>> validate('100 000 008')
Traceback (most recent call last):
...
InvalidComponent: ...
>>> format('988077917')
'988 077 917'
"""
Expand Down Expand Up @@ -64,6 +70,8 @@ def validate(number: str) -> str:
raise InvalidFormat()
if len(number) != 9:
raise InvalidLength()
if number[0] not in '89':
raise InvalidComponent('The number must start with 8 or 9.')
if checksum(number) != 0:
raise InvalidChecksum()
return number
Expand Down
71 changes: 71 additions & 0 deletions tests/test_no_orgnr.doctest
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
test_no_orgnr.doctest - more detailed doctests for stdnum.no.orgnr module

Copyright (C) 2026 M J Anthony

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see <https://www.gnu.org/licenses/>.


This file contains more detailed doctests for the stdnum.no.orgnr module. It
tries to cover more corner cases and detailed functionality that is not
really useful as module documentation.

>>> from stdnum.no import orgnr


These have been found online and should all be valid numbers.

>>> numbers = '''
...
... 988 077 917
... 965 920 358
... 980 430 596
... 998 772 680
... 987 008 644
... 917 313 008
... 966 813 946
... 982 930 057
... 982 952 573
... 987 989 297
...
... '''
>>> [x for x in numbers.splitlines() if x and not orgnr.is_valid(x)]
[]


Norwegian organisation numbers always start with 8 or 9. Each of the
following numbers has a valid modulus 11 check digit but an invalid first
digit, so they must all be rejected.

>>> numbers = '''
...
... 000 000 000
... 100 000 008
... 200 000 005
... 300 000 002
... 410 000 008
... 500 000 007
... 600 000 004
... 700 000 001
...
... '''
>>> [x for x in numbers.splitlines() if x and orgnr.is_valid(x)]
[]


Numbers that start with 8 or 9 with a correct check digit remain valid.

>>> orgnr.validate('800 000 009')
'800000009'
>>> orgnr.validate('900 000 006')
'900000006'
Loading