Skip to content

Instantly share code, notes, and snippets.

@flowtwo
Created March 21, 2017 10:04
Show Gist options
  • Save flowtwo/5c74c164ec43705947a50b33088c0dfb to your computer and use it in GitHub Desktop.
Save flowtwo/5c74c164ec43705947a50b33088c0dfb to your computer and use it in GitHub Desktop.
Validate Danish CPR
// Validate CPR-nr.
function is_cpr_valid(cpr) {
var res = cpr.replace(/[^0-9]/ig, "");
if (res.length != 10) {
return false;
}
var validDates = ['010165', '010166', '010170', '010180', '010187', '010190'];
var value = res.substr(0, 6);
if (contains(validDates, value)) {
return true;
}
var cpr_arr = res.split('');
var mul_arr = "4327654321".split('');
var sum = 0;
for (var i = 0; i < cpr_arr.length; i++) {
sum += cpr_arr[i] * mul_arr[i];
}
if (sum % 11 == 0) {
return true;
} else {
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment