Skip to content

Instantly share code, notes, and snippets.

@imsickofmaps
Created April 22, 2014 10:26
Show Gist options
  • Save imsickofmaps/11173313 to your computer and use it in GitHub Desktop.
Save imsickofmaps/11173313 to your computer and use it in GitHub Desktop.
function validateSAID(idnum) {
idnum = idnum.toString().replace(" ", "");
r = /^\d{10}[0-1]\d{2}$/;
if (!r.test(idnum)) return false;
n = idnum;
p1 = parseInt(n[0]) + parseInt(n[2]) + parseInt(n[4]) + parseInt(n[6]) + parseInt(n[8]) + parseInt(n[10]);
p2 = (parseInt(n[1] + n[3] + n[5] + n[7] + n[9] + n[11]) * 2).toString();
p3 = 0;
for (i = 0; i < p2.length; i++) {
p3 += parseInt(p2[i]);
}
check = 10 - (p1 + p3).toString()[(p1 + p3).toString().length - 1];
check_char = check > 9 ? check.toString()[1] : check.toString();
if (check_char != idnum[12]) return false;
return true;
}
@imsickofmaps
Copy link
Author

@imsickofmaps
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment