Skip to content

Instantly share code, notes, and snippets.

@hillerstorm
Forked from 140bytes/LICENSE.txt
Last active September 30, 2022 09:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hillerstorm/6816840 to your computer and use it in GitHub Desktop.
Save hillerstorm/6816840 to your computer and use it in GitHub Desktop.
Swedish social security number validator

Swedish social security number validator

A Swedish social security number is composed of 10 digits in two groups, 6 and 4 digits each. The groups are separated by a dash (changes to a + when a person is 100 years old or more).

Birth date  Checksum
   ||||||    |
   811218-9876
          |||
        "Birthnumber"

The first 6 digits are the birth date (yyMMdd). The next 3 digits is an identifier where the last digit indicates the persons gender - even numbers for women, odd for men. The last digit is a checksum, calculated using the Luhn algorithm.

Example of applying the algorithm on the social security number above:

                     8 1 1 2 1 8 - 9 8  7 x
Double every other  16 1 2 2 2 8  18 8 14 x
Sum of digits        7 1 2 2 2 8   9 8  5 =44

This sum you multiply by 9 (44 * 9 = 396) and the last digit, 6 in this case, is your checksum.

PS! This doesn't check for a valid birth date, just the format and checksum.

function(
a, // social security number
b, // placeholder
c, // --||--
d // --||--
){
c='';
// we're only interested in the first 10 chars (9 digits)
// using a second index to skip the dash while still maintaining
// an index to check for even/odd
for(b=d=0;b<10;b++)
c+=b!=6 // if we're not on the dash
?a[b]*(d++%2||2) // multiply the value with 2 if needed
:''; // skip the dash
b=0; // reset the index to use as a sum
// sum all the digits
for(d in c)
b+=c[d]*1;
// multiply the sum by 9 and check if the last digit of the checksum
// matches the last digit of the input
return b*9%10==a[10]
}
function(a,b,c,d){c='';for(b=d=0;b<10;b++)c+=b!=6?a[b]*(d++%2||2):'';b=0;for(d in c)b+=c[d]*1;return b*9%10==a[10]}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2013 Johan Hillerström <https://github.com/hillerstorm>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "swedishSocialSecurityNumberValidator",
"description": "Validates the format and checksum of a Swedish social security number.",
"keywords": [
"validation",
"swedish",
"socialsecuritynumber"
]
}
<!DOCTYPE html>
<title>Swedish social security number validator</title>
<div>Expected value: <b>true</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
var validate = function(a,b,c,d){c='';for(b=d=0;b<10;b++)c+=b!=6?a[b]*(d++%2||2):'';b=0;for(d in c)b+=c[d]*1;return b*9%10==a[10]}
document.getElementById('ret').innerHTML = validate('811218-9876')
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment