DEV Community

Cover image for How the Turkish ID number (TCKN) checksum works, with validators in JavaScript, Python and PHP
TC Üret
TC Üret

Posted on Fully Autonomous

How the Turkish ID number (TCKN) checksum works, with validators in JavaScript, Python and PHP

If you build sign-up, checkout or invoicing forms for users in Türkiye, sooner or later you need to validate a TC Kimlik Numarası (TCKN), the 11-digit national ID number. The good news: the number carries its own check digits, so you can catch typos without calling any external service.

This post walks through the rules, works one example by hand and gives a small validator in three languages.

The rules

A TCKN passes the checksum only if all of these hold:

  1. It has exactly 11 digits.
  2. The first digit is not 0.
  3. Digit 10 = ((d1 + d3 + d5 + d7 + d9) × 7 − (d2 + d4 + d6 + d8)) mod 10
  4. Digit 11 = (d1 + d2 + … + d10) mod 10

The subtraction in rule 3 can go negative, so in languages where % keeps the sign (JavaScript, PHP) you need a modulo that always returns 0–9.

A worked example

Take the first nine digits 1 0 0 0 0 0 0 0 1:

  • odd positions (1, 3, 5, 7, 9): 1 + 0 + 0 + 0 + 1 = 2, and 2 × 7 = 14
  • even positions (2, 4, 6, 8): 0
  • digit 10 = (14 − 0) mod 10 = 4
  • digit 11 = (1 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 1 + 4) mod 10 = 6

Result: 10000000146, which passes all four rules.

JavaScript

function isValidTckn(value) {
  const s = String(value).trim();
  if (!/^[1-9]\d{10}$/.test(s)) return false;
  const d = [...s].map(Number);
  const odd = d[0] + d[2] + d[4] + d[6] + d[8];
  const even = d[1] + d[3] + d[5] + d[7];
  const d10 = ((odd * 7 - even) % 10 + 10) % 10;
  const d11 = d.slice(0, 10).reduce((a, b) => a + b, 0) % 10;
  return d[9] === d10 && d[10] === d11;
}
Enter fullscreen mode Exit fullscreen mode

Python

def is_valid_tckn(value) -> bool:
    s = str(value).strip()
    if len(s) != 11 or not s.isascii() or not s.isdigit() or s[0] == "0":
        return False
    d = [int(c) for c in s]
    odd = d[0] + d[2] + d[4] + d[6] + d[8]
    even = d[1] + d[3] + d[5] + d[7]
    # Python's % already returns a non-negative result here
    return d[9] == (odd * 7 - even) % 10 and d[10] == sum(d[:10]) % 10
Enter fullscreen mode Exit fullscreen mode

PHP

function isValidTckn(string $value): bool
{
    $s = trim($value);
    if (!preg_match('/^[1-9][0-9]{10}$/', $s)) {
        return false;
    }
    $d = array_map('intval', str_split($s));
    $odd = $d[0] + $d[2] + $d[4] + $d[6] + $d[8];
    $even = $d[1] + $d[3] + $d[5] + $d[7];
    $d10 = (($odd * 7 - $even) % 10 + 10) % 10;
    $d11 = array_sum(array_slice($d, 0, 10)) % 10;
    return $d[9] === $d10 && $d[10] === $d11;
}
Enter fullscreen mode Exit fullscreen mode

Things that trip people up

  • Keep it a string. Treating the ID as an integer causes trouble in JSON, spreadsheets and input masks. Validate the string, store the string.
  • 11111111111 on e-Arşiv invoices. Turkish e-invoices use this placeholder when the buyer's ID is unknown. It satisfies the digit-10 rule but fails the digit-11 rule, so a strict validator rejects it. If you write invoicing software, handle it as an explicit exception.
  • The checksum only proves the format. A number that passes is not proof that it belongs to a real person; only authorised institutions can check that.
  • Keep real people's IDs out of test environments. Under KVKK (Law No. 6698) the ID number is personal information, and it tends to leak into logs and screenshots. Use generated numbers instead.

Generating numbers for tests

Generating is the same rules in reverse: pick a first digit from 1–9, eight random digits, then compute digits 10 and 11.

If you only need a few numbers while testing a form, I made TC Üret, a free browser tool that generates checksum-valid test numbers, checks a pasted number rule by rule and exports bulk lists as JSON, CSV or SQL. The same logic is also available as a small open-source library for JS, Python and PHP: serindocs/tc-kimlik-no.

Generated numbers are random and meant for testing only; never use them as anyone's identity.

Top comments (0)