Last active
August 29, 2015 14:22
-
-
Save johanhalse/36e5a13e1ac3ad56e195 to your computer and use it in GitHub Desktop.
Validate a Swedish personnummer with Luhn's Algorithm
This file contains 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
// 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