Skip to content

Instantly share code, notes, and snippets.

@teyfix
Created April 13, 2025 20:36
Show Gist options
  • Save teyfix/980ebbc07801d92b2828b4e452a7a9fa to your computer and use it in GitHub Desktop.
Save teyfix/980ebbc07801d92b2828b4e452a7a9fa to your computer and use it in GitHub Desktop.
Turkish National Identity Validation • TC Kimlik Numarası Doğrulama
/**
* Returns true if the given string is a valid Turkish national identity number.
*
* @remarks
* This implementation uses the algorithm described on the Wikipedia page
* "Turkish Identification Number".
* @param value - The string to check.
* @returns True if the string is a valid Turkish national identity number, false otherwise.
*/
const IsNationalIdentity = (value: string): boolean => {
if (
value == null ||
typeof value !== 'string' ||
value.length !== 11 ||
/\D/.test(value)
) {
return false;
}
let sum = 0;
let sumOdd = 0;
let sumEven = 0;
let c1 = 0;
let c2 = 0;
for (let i = 0; i < value.length; i++) {
const e = Number.parseInt(value[i]);
if (i === 9) {
c1 = e;
} else if (i === 10) {
c2 = e;
} else {
sum += e;
if (i % 2 === 0) {
sumEven += e;
} else {
sumOdd += e;
}
}
}
return c1 === (sumEven * 7 - sumOdd) % 10 && c2 === (sum + c1) % 10;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment