Last active
December 17, 2015 11:19
-
-
Save netologist/5601623 to your computer and use it in GitHub Desktop.
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
package com.hasanozgan.utils; | |
/** | |
* Algoritma | |
* a] 1. 3. 5. 7. ve 9. basamaklarının sayı değerleri toplamının 7 katından, 2. 4. 6. ve 8. basamaklarının sayı değerleri toplamı çıkartılır. Elde edilen sonucun 10′a bölümünden kalan(yani mod10) kimlik numaralarımızın 10. basamağına eşittir. | |
* b] 1. 2. 3. 4. 5. 6. 7. 8. 9. ve 10. basamaklarının sayı değerleri toplamı 10′a bölünür. Elde edilen kalan(yani mod10) bize kimlik numaralarımızın 11. basamağını verir. | |
*/ | |
public class TurkeyCitizenshipIdentityValidator { | |
final static String TC_KIMLIK_NO_PATTERN = "[1-9][0-9]{10}"; | |
public static boolean isValid(String tcNumber) | |
{ | |
if (!tcNumber.matches(TC_KIMLIK_NO_PATTERN)) { return false; } | |
int total = 0, | |
oddDigitPosition = 0, | |
evenDigitPosition = 0, | |
digitN = 0, | |
digit10 = Character.getNumericValue(tcNumber.charAt(9)), | |
digit11 = Character.getNumericValue(tcNumber.charAt(10)); | |
for (int i = 0; i < 10; i++) { | |
total += digitN = Character.getNumericValue(tcNumber.charAt(i)); | |
if ((i % 2) == 0) { oddDigitPosition += digitN; } | |
else if (i < 9) { evenDigitPosition += digitN; } | |
} | |
return (((total % 10) == digit11) && ((((oddDigitPosition * 7) - evenDigitPosition) % 10) == digit10)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment