Skip to content

Instantly share code, notes, and snippets.

@BurakDizlek
Created October 16, 2019 13:20
Show Gist options
  • Save BurakDizlek/0a5a4220e45c5f8e799a0e3818ef1a8b to your computer and use it in GitHub Desktop.
Save BurakDizlek/0a5a4220e45c5f8e799a0e3818ef1a8b to your computer and use it in GitHub Desktop.
fun String.isValidTcNo(): Boolean {
val tcNo = this.trim()
if (tcNo.length != 11)
return false
if (tcNo.startsWith("0"))
return false
if (!tcNo.matches("[0-9]+".toRegex()))
return false
val tcNoArray = tcNo.map { it.toString().toInt() }
if (tcNoArray[9] != (((7 * (tcNoArray[0] + tcNoArray[2] + tcNoArray[4] + tcNoArray[6]+ tcNoArray[8])) - (tcNoArray[1] + tcNoArray[3]+ tcNoArray[5] + tcNoArray[7])) % 10))
return false
var totalFirst10 = 0
for (x in 0 until 10) {
totalFirst10 += tcNoArray[x]
}
if (tcNoArray[10] != (totalFirst10 % 10))
return false
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment