Skip to content

Instantly share code, notes, and snippets.

@FromeXo
Last active December 4, 2018 00:37
Show Gist options
  • Select an option

  • Save FromeXo/c56063962e0544d3c686f65a35d34e26 to your computer and use it in GitHub Desktop.

Select an option

Save FromeXo/c56063962e0544d3c686f65a35d34e26 to your computer and use it in GitHub Desktop.
/*******************************************************************************
* Luhn-algoritmen
*
* Calculate the controll digit in a swedish social security number.
*
* @param String ssn 12 digit ssn with a dash befor the 4 last digits.
******************************************************************************/
function checkLastDigit (ssn) {
// Remove the dash from ssn
ssn = ssn.replace('-', '');
// Pop of the last number of ssn and parse it as a interger.
var lastDigit = parseInt(ssn.substring(( ssn.length - 1 )));
// After last digit has been extraced remove it from the string.
// Also remove the 2 first digit from the beginning of the string (19|20).
ssn = ssn.substring(2, ( ssn.length - 1 ));
// Calculate the products and concatenate them.
var products;
for (var i=0; ssn.length > i; i++ ) {
products += (i % 2) == 0 ? (ssn[i] * 2).toString() : (ssn[i] * 1).toString();
}
// Calculate the sum of all products.
var sum = 0;
for ( var j=0; result.length > j; j++ ) {
sum += parseInt(result[j]);
}
// Substract 10 and modulo 10 twice.
return ((10 - (sum % 10)) % 10) === lastDigit ? true : false;
}
function getSex(ssn) {
if ( (parseInt(ssn.substring(11, 12)) % 2) == 0) {
return 'female';
} else {
return 'male';
}
}
function isSsn(ssn) {
var pattern = /(19|20)\d{2}((0(1|3|5|7|8)|1(0|2))(0[1-9]|1[0-9]|2[0-9]|3[0-1])|02(0[1-9]|1[0-9]|2[0-9])|(0(4|6|9)|11)(0[1-9]|1[0-9]|2[0-9]|30))\-\d{4}/;
return pattern.test(ssn);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment