@@ -107,15 +107,16 @@ export function validateObosMembershipNumber(
107107type PersonalIdentityNumberOptions = ValidatorOptions ;
108108
109109/**
110- * Validates that the input value is a Norwegian personal identity number.
110+ * Validates that the input value is a Norwegian national identity number.
111111 *
112- * Supports both Fødselsnummer, D-numbers and H-numbers .
112+ * Supports both fødselsnummer and d-number .
113113 * @example
114114 * ```
115- * validatePersonalIdentityNumber('0000000') // => true
115+ * validatePersonalIdentityNumber('21075417753') // => true
116+ * validatePersonalIdentityNumber('61075440676') // => true
116117 * ```
117118 */
118- export function validatePersonalIdentityNumber (
119+ export function validateNationalIdentityNumber (
119120 value : string ,
120121 options : PersonalIdentityNumberOptions = { } ,
121122) : boolean {
@@ -124,6 +125,7 @@ export function validatePersonalIdentityNumber(
124125 value = stripFormatting ( value ) ;
125126 }
126127
128+ // Fødselsn
127129 // Fødselsnummer and d numbers have two control digits.
128130 // where the first one is calculated using the first 10 digits.
129131 const valueForControlDigit1 = value . slice ( 0 , - 1 ) ;
@@ -133,23 +135,18 @@ export function validatePersonalIdentityNumber(
133135 ) ;
134136 const controlDigit2 = mod11 ( value , [ 5 , 4 , 3 , 2 , 7 , 6 , 5 , 4 , 3 , 2 ] ) ;
135137
136- if ( ! controlDigit1 && controlDigit2 ) {
138+ if ( ! controlDigit1 && ! controlDigit2 ) {
137139 return false ;
138140 }
139141
140142 let day = Number ( value . substring ( 0 , 2 ) ) ;
141- let month = Number ( value . substring ( 2 , 4 ) ) ;
143+ const month = Number ( value . substring ( 2 , 4 ) ) ;
142144 const year = Number ( value . substring ( 4 , 6 ) ) ;
143145
144- switch ( true ) {
145- // d number
146- case value [ 0 ] >= 4 :
147- day = day - 40 ;
148- break ;
149- // h number
150- case value [ 2 ] >= 4 :
151- month = month - 40 ;
152- break ;
146+ // 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.
147+ // thus we need to subtract 40 to get the correct day of the month
148+ if ( day >= 40 ) {
149+ day = day - 40 ;
153150 }
154151
155152 const date = new Date ( Date . UTC ( year , month - 1 , day ) ) ;
0 commit comments