Skip to content

Instantly share code, notes, and snippets.

@stpe
Last active February 17, 2020 20:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stpe/aebc00b9ad361d93f33d to your computer and use it in GitHub Desktop.
Save stpe/aebc00b9ad361d93f33d to your computer and use it in GitHub Desktop.
Validate Swedish Personal Identity Number (personnummer) using checksum
// 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