Skip to content

Instantly share code, notes, and snippets.

@MiktatMertCento
Created May 10, 2022 11:05
Show Gist options
  • Save MiktatMertCento/aafac728054b83e477500e2bb220bb94 to your computer and use it in GitHub Desktop.
Save MiktatMertCento/aafac728054b83e477500e2bb220bb94 to your computer and use it in GitHub Desktop.
Turkish: Javascript tc kimlik no doğrulama, English: Javascript turkish identity number validator.
export function tcNoValidate(tcNo) {
tcNo = tcNo.toString();
if (tcNo.length !== 11) return false;
if (tcNo[0] === '0') return false;
let tekToplam = 0, ciftToplam = 0, genelToplam = 0;
for (let i = 0; i < tcNo.length - 2; i++) {
if (i % 2 === 0) {
tekToplam += parseInt(tcNo[i]);
} else {
ciftToplam += parseInt(tcNo[i]);
}
genelToplam += parseInt(tcNo[i])
}
if (((tekToplam * 7) - ciftToplam) % 10 !== parseInt(tcNo[tcNo.length - 2])) return false;
genelToplam += parseInt(tcNo[9]);
if (genelToplam % 10 !== parseInt(tcNo[10])) return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment