Created
August 18, 2020 15:41
-
-
Save jesualex/dd5778b565ea7fa3a431f6c950a24047 to your computer and use it in GitHub Desktop.
UY CI validation for kotlin
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
fun getUyValDigit(externCi: String): Int{ | |
if(externCi.isEmpty() || externCi.length > 7) return 0 | |
var ci = externCi | |
val validationArray = arrayOf(2,9,8,7,6,3,4) | |
var resp = 0 | |
while (ci.length <= 6) { | |
ci = "0$ci" | |
} | |
for(i in ci.indices){ | |
val char = ci[i] | |
if(char.isDigit()){ | |
resp += (Character.getNumericValue(ci[i]) * validationArray[i]) % 10 | |
} | |
} | |
return if(resp % 10 == 0) 0 else 10 - resp % 10 | |
} | |
fun validateUyCi(ci: String): Boolean{ | |
if(ci.isEmpty()) return false | |
var cleanCi = cleanUyCi(ci) | |
val dig = ci.last() | |
cleanCi = cleanCi.replace(Regex.fromLiteral("/[0-9]$/"), "") | |
return dig.isDigit() && Character.getNumericValue(dig) == getUyValDigit(cleanCi) | |
} | |
fun cleanUyCi(ci: String): String{ | |
return if(ci.isEmpty()) ci else ci.dropLast(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment