本文介绍了Swift中的土耳其身份证号码验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定给定的文本是土耳其身份证号?我在这里看到了 js版本此处的Phthon版本

How can I make sure that the given text is Turkish Identity Number?I have seen js version here and phthon version here

土耳其语身份验证不仅仅检查其数字,它还具有其他功能.让我更清楚一点,它是数字,有11位数字.例如,假设前9位数字用d表示,后一位数字用c:

Turkish Identity Verification is not checks only if its numeric, it has some other functions too. Let me be more clear, It is numeric and has 11 digits. For example Let assume that first 9 digits are represented by d, and the last ones represented by c:

Identity Number = d1 d2 d3 d4 d5 d6 d7 d8 d9 c1 c2

第10个数字必须是

c1 = ( (d1 + d3 + d5 + d7 + d9) * 7 - (d2 + d4 + d6 + d8) ) mod10

第11个必须是

c2 = ( d1 + d2 + d3 + d4 + d5 + d6 + d7 + d8 + d9 + c1 ) mod10

,且永远不会以"0"开头例如,"87836910956"是土耳其身份证号码.

and it never starts with "0"For example "87836910956" is a Turkish Identity Number.

推荐答案

一个简单的实现:

func verifyNumber(_ number: String) -> Bool {
    let pattern = "^[1-9][0-9]{10}$"

    // test that the length is correct and it's composed from digits
    guard number.range(of: pattern, options: .regularExpression) != nil else {
        return false
    }

    // convert characters to numbers
    let digits: [Int] = number.compactMap { Int(String($0)) }
    // split digits and check digits
    let d = Array(digits.prefix(9))
    let c = Array(digits.suffix(2))

    // calculate check digits
    let c1 = ((d[0] + d[2] + d[4] + d[6] + d[8]) * 7 - (d[1] + d[3] + d[5] + d[7])) % 10
    let c2 = (d.reduce(0, +) + c1) % 10

    // validate check digits    
    return c[0] == c1 && c[1] == c2
}

这篇关于Swift中的土耳其身份证号码验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-20 23:11