Simple ID verification

 

 

kotlin code:

 

    private fun validateIdCard(cardNo: String?): Boolean {
        cardNo ?: return false
        if (cardNo.length != 18 || !cardNo.substring(0, 17).matches(Regex("\\d{17}"))) {
            return false
        }
        // The first 17 digits of the ID number are multiplied by the corresponding values ​​in the following array and then summed
        // Then the result after modulo 11 is used as an array subscript, and go to validCodes to get the correct tail number and the tail number in the parameter for verification.
        val intArr = arrayOf(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
        var sum = 0
        for (idx in intArr.indices) {
            sum += Character.digit(cardNo[idx], 10) * intArr[idx]
        }
        val mod = sum % 11
        val validCodes = arrayOf("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2")
        return validCodes[mod] == cardNo.substring(cardNo.length - 1)
    }

 

Only calculate the correct tail number based on the first 17 digits to verify

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326988524&siteId=291194637