-
-
Save farukcam/b7fc6b538da530800bf10788de7d4515 to your computer and use it in GitHub Desktop.
Validate TC ID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//======================================= | |
// 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 ---------// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment