Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Validate TC ID
//=======================================
// CHECK TC ID =
//=======================================
var checkTcNum = function(value) {
value = value.toString();
var isEleven = /^[0-9]{11}$/.test(value);
var totalX = 0;
for (var i = 0; i < 10; i++) {
totalX += Number(value.substr(i, 1));
}
var isRuleX = totalX % 10 == value.substr(10,1);
var totalY1 = 0;
var totalY2 = 0;
for (var i = 0; i < 10; i+=2) {
totalY1 += Number(value.substr(i, 1));
}
for (var i = 1; i < 10; i+=2) {
totalY2 += Number(value.substr(i, 1));
}
var isRuleY = ((totalY1 * 7) - totalY2) % 10 == value.substr(9,0);
return isEleven && isRuleX && isRuleY;
};
window.checkTcNum = checkTcNum;
//-------- End of check tc id ---------//
@muratbeser

This comment has been minimized.

Copy link

@muratbeser muratbeser commented Jul 22, 2016

eğer ki value undefined olarak gelirse javascript error throw edecektir, value = String(value) herhangi bir error throw etmeden doğrulamayı gerçekleşecektir.

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.