T.R. Is ID number validated with regex? Format and algorithm control
T.R. check the 11-digit format of the ID number with regex, the last two check digits with the algorithm; Know the limits of verification and KVKK requirements.
T.R. identification numberFor regex can only check that the value consists of 11 digits and the first digit is not zero. The last two digits are the control values calculated from the previous digits; algorithm is required besides regex to verify them.
A number passed through the algorithm does not alone prove that it belongs to a real person. Authorized service and legal basis should be used in processes that require official identity verification; unnecessary T.R. identification number should not be collected.
Regex for format control
^[1-9]\d{10}$
This expression limits the first digit to 1–9 and the remaining ten characters as digits. It does not accept spaces, dashes or letters. Processing the ID number as a string instead of storing it as a number for mathematical operations makes the leading character and length checks clearer.
Check digit algorithm
When we number the digits from 1 to 11 from left to right:
- 10th step; It is found by subtracting the sum of the 2nd, 4th, 6th and 8th digits from seven times the sum of the 1st, 3rd, 5th, 7th and 9th digits, and taking the remainder of the result according to 10.
- The 11th digit is the remainder of the sum of the first ten digits divided by 10.
To avoid negative mode behavior in JavaScript, it is necessary to first move the result to the positive range.
JavaScript validation function
function validateTckn(value) {
const text = String(value);
if (!/^[1-9]\d{10}$/.test(text)) return false;
const d = [...text].map(Number);
const oddSum = d[0] + d[2] + d[4] + d[6] + d[8];
const evenSum = d[1] + d[3] + d[5] + d[7];
const digit10 = ((oddSum * 7 - evenSum) % 10 + 10) % 10;
if (d[9] !== digit10) return false;
const firstTenSum = d.slice(0, 10)
.reduce((total, digit) => total + digit, 0);
return d[10] === firstTenSum % 10;
}
The function checks form and mathematical consistency. Do not use numbers belonging to real people in tests. Test negative scenarios with invalid length, values starting with zero, values containing letters, and synthetic inputs with corrupted check digits.
Limit of algorithm verification
Check digits are designed to catch some spelling errors. A combination passed through the algorithm may not appear in the civil registry; Additionally, it does not indicate that a number found belongs to the person filling out the form. If identity matching is required, use authoritative authentication mechanism with clear business purpose and appropriate access control.
Data minimization in terms of KVKK
T.R. ID number is personal data. When designing the form, determine whether this field is really necessary, what legal reason it is based on, how long it will be stored and who will access it. Collecting “just in case” is not the right approach.
Do not write the number in application logs, analysis tools, error screens, or the URL parameter. If retention is required, implement TLS in transit, minimum authorization on access, recorded access, and appropriate encryption/key management. Show masked value instead of the whole in support screens.
Frontend control is not enough
Regex and algorithm provide fast feedback in the browser, but the request can be sent directly to the API. The backend should run the same checks again and return the error with a general validation message. If a uniqueness rule is required in the database, it should be defined separately according to the nature of the business process.
Short guide
Common mistake
Mistaking a regex matching 11 digits for official authentication or using real people's numbers as test data.