Skip to content

Instantly share code, notes, and snippets.

@onury onury/tcno.js

Last active Jan 26, 2020
Embed
What would you like to do?
Turkish Identity Number validation (TC Kimlik No)
tcno(value) {
value = String(value);
// T.C. identity number should have 11 digits and first should be non-zero.
if (!(/^[1-9]\d{10}$/).test(value)) return false;
var digits = value.split(''),
// store last 2 digits (10th and 11th) which are actually used for validation
d10 = Number(digits[9]),
d11 = Number(digits[10]),
// we'll also need the sum of first 10 digits for validation
sumOf10 = 0,
evens = 0,
odds = 0;
digits.forEach(function (d, index) {
d = Number(d);
if (index < 10) sumOf10 += d;
if (index < 9) {
if ((index + 1) % 2 === 0) {
evens += d;
} else {
odds += d;
}
}
});
// check if the unit-digit of the sum of first 10 digits equals to the 11th digit.
if (sumOf10 % 10 !== d11) return false;
// check if unit-digit of the sum of odds * 7 and evens * 9 is equal to 10th digit.
if (((odds * 7) + (evens * 9)) % 10 !== d10) return false;
// check if unit-digit of the sum of odds * 8 is equal to 11th digit.
if ((odds * 8) % 10 !== d11) return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.