Skip to content

Commit b071f1e

Browse files
committed
changeset
1 parent 2ba3dd6 commit b071f1e

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

.changeset/shy-heads-cross.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
"@obosbbl/validation": minor
3+
---
4+
5+
add method `validateNationalIdentityNumber()`
6+
7+
Validates that the input is a valid Norwegian national identity number (either a fødselsnummer or a D-nummer).
8+
It validates the checksum and checks if the date of birth is valid.
9+
10+
```
11+
import { validateNationalIdentityNumber } from "@obosbbl/validation/no";
12+
13+
// Fødselsnummer
14+
validatePersonalIdentityNumber('21075417753') // => true
15+
16+
// D-nummer
17+
validatePersonalIdentityNumber('53097248016') // => true
18+
```

packages/validation/src/no.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ export function validateObosMembershipNumber(
107107
type PersonalIdentityNumberOptions = ValidatorOptions;
108108

109109
/**
110-
* Validates that the input value is a Norwegian national identity number.
110+
* Validates that the input value is a Norwegian national identity number (fødselsnummer or d-nummer).
111+
*
112+
* It validates the control digits and checks if the date of birth is valid.
111113
*
112-
* Supports both fødselsnummer and d-nummer.
113114
* @example
114115
* ```
115116
* // Fødselsnummer
@@ -143,18 +144,20 @@ export function validateNationalIdentityNumber(
143144
return false;
144145
}
145146

147+
// copy/inspiration from NAV https://github.com/navikt/fnrvalidator/blob/77e57f0bc8e3570ddc2f0a94558c58d0f7259fe0/src/validator.ts#L108
146148
let day = Number(value.substring(0, 2));
147149
const month = Number(value.substring(2, 4));
148150
let year = Number(value.substring(4, 6));
149151

150-
// 1900 isn't a leap year
152+
// 1900 isn't a leap year, but 2000 is. Since JS two digits years to the Date constructor is an offset from the year 1900
153+
// we need to special handle that case. For other cases it doesn't really matter if the year is 1925 or 2025.
151154
if (year === 0) {
152155
year = 2000;
153156
}
154157

155-
// for a d-number the first digit is increased by 4. Eg the 31st of a month would be 71, or the 3rd would be 43.
158+
// for a d-number the day is increased by 40. Eg the 31st of a month would be 71, or the 3rd would be 43.
156159
// thus we need to subtract 40 to get the correct day of the month
157-
if (day >= 40) {
160+
if (day > 40) {
158161
day = day - 40;
159162
}
160163

0 commit comments

Comments
 (0)