Skip to content

Instantly share code, notes, and snippets.

@johanhalse
Last active August 29, 2015 14:22
Show Gist options
  • Save johanhalse/36e5a13e1ac3ad56e195 to your computer and use it in GitHub Desktop.
Save johanhalse/36e5a13e1ac3ad56e195 to your computer and use it in GitHub Desktop.
Validate a Swedish personnummer with Luhn's Algorithm
// Expects a Swedish personnummer formatted as YYYYMMDDXXXX
var isLuhn = function(pnr) {
var multiplier = 1;
var nums = pnr.substr(2).split('').map(function(numStr) {
multiplier = 1 + (multiplier % 2);
var num = parseInt(numStr) * multiplier;
return (num < 10) ? num : num - 9;
});
return nums.reduce(function(prev, curr) { return curr + prev; }) % 10 === 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment