Skip to content

Instantly share code, notes, and snippets.

@jesualex
Created August 18, 2020 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesualex/dd5778b565ea7fa3a431f6c950a24047 to your computer and use it in GitHub Desktop.
Save jesualex/dd5778b565ea7fa3a431f6c950a24047 to your computer and use it in GitHub Desktop.
UY CI validation for kotlin
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