How to validate iranian Melli Code (National Code or Code Melli) in android

前端 未结 7 1351
花落未央
花落未央 2021-02-19 09:25

I need a method to validate the Melli Code (National Code or Code Melli) of iranian people.

I Know it has a formula.

7条回答
  •  不知归路
    2021-02-19 09:56

    I have so many different versions of it here but the following is its Kotlin version,

    fun isValidIranianNationalCode(input: String): Boolean {
      if (!"^\\d{10}$".toRegex().matches(input))
        return false
    
      input.toCharArray().map(Character::getNumericValue).let {
        val check = it[9]
        val sum = (0..8).map { i -> it[i] * (10 - i) }.sum() % 11
        return sum < 2 && check == sum || sum >= 2 && check + sum == 11
      }
    }
    

提交回复
热议问题