Last active
February 17, 2020 20:20
-
-
Save stpe/aebc00b9ad361d93f33d to your computer and use it in GitHub Desktop.
Validate Swedish Personal Identity Number (personnummer) using checksum
This file contains hidden or 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 Swedish Personal Identity Number (personnummer) using checksum | |
// note: this is somewhat simplified because it does not take into account | |
// that the date of the number is valid (e.g. "000000-0000" does return as true) | |
function isValidSwedishPIN(pin) { | |
pin = pin | |
.replace(/\D/g, "") // strip out all but digits | |
.split("") // convert string to array | |
.reverse() // reverse order for Luhn | |
.slice(0, 10); // keep only 10 digits (i.e. 1977 becomes 77) | |
// verify we got 10 digits, otherwise it is invalid | |
if (pin.length != 10) { | |
return false; | |
} | |
var sum = pin | |
// convert to number | |
.map(function(n) { | |
return Number(n); | |
}) | |
// perform arithmetic and return sum | |
.reduce(function(previous, current, index) { | |
// multiply every other number with two | |
if (index % 2) current *= 2; | |
// if larger than 10 get sum of individual digits (also n-9) | |
if (current > 9) current -= 9; | |
// sum it up | |
return previous + current; | |
}); | |
// sum must be divisible by 10 | |
return 0 === sum % 10; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment