Swift中的土耳其身份号码验证

Swift中的土耳其身份号码验证,swift,function,numeric,digits,turkish,Swift,Function,Numeric,Digits,Turkish,如何确保给定的文本是土耳其身份号码? 我见过,也见过 土耳其身份验证不是只检查其数字,它还有一些其他功能。 让我更清楚一点,它是数字的,有11位数字。例如,假设前9位数字由d表示,最后9位数字由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 必须是 c2 = ( d1 + d2

如何确保给定的文本是土耳其身份号码? 我见过,也见过

土耳其身份验证不是只检查其数字,它还有一些其他功能。 让我更清楚一点,它是数字的,有11位数字。例如,假设前9位数字由d表示,最后9位数字由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
必须是

c2 = ( d1 + d2 + d3 + d4 + d5 + d6 + d7 + d8 + d9 + c1 ) mod10
而且它从不以“0”开头
例如,“87836910956”是一个土耳其身份号码。

通过使用
guard
可以消除所有这些
的深层嵌套,如果
s:

func isValidIdentityNumber(_ value: String) -> Bool {
    guard
        value.count == 11,
        let digits = value.map({ Int(String($0)) }) as? [Int],
        digits[0] != 0
    else { return false }

    let check1 = (
        (digits[0] + digits[2] + digits[4] + digits[6] + digits[8]) * 7
        - (digits[1] + digits[3] + digits[5] + digits[7])
    ) % 10

    guard check1 == digits[9] else { return false }

    let check2 = (digits[0...8].reduce(0, +) + check1) % 10

    return check2 == digits[10]
}

请注意,如果由于
map
包含
nil
的结果,
value
中的任何字符无法转换为
Int
,则第4行的强制转换将失败。这只是一个简单的实现:

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
}
你可以看看我的要点,其他人认为它比较短

func validateID(_ id: String) -> Bool {
 let digits = id.map {Int(String($0))} as! [Int]
 guard digits.count == 11, digits[0] != 0, digits[9] != 0 else { return false }
 let firstValidation = (digits[0] + digits[2] + digits[4] + digits[6] + digits[8]) * 7
 let secondValidation = digits[1] + digits[3] + digits[5] + digits[7]

 let tenthDigit = (firstValidation - secondValidation) % 10
 let eleventhDigit = (digits.reduce(0, +) - digits[10]) % 10
 return (digits[9] == tenthDigit && digits[10] == eleventhDigit) ? true: false
}

//usage
validateID("49673861228") //returns true and generated from https://www.simlict.com/
这篇文章可能对你有帮助

public class func validateCitizenshipID(ID: Int) -> Bool {
let digits = ID.description.characters.map { Int(String($0)) ?? 0 }

if digits.count == 11
{
    if (digits.first != 0)
    {
        let first   = (digits[0] + digits[2] + digits[4] + digits[6] + digits[8]) * 7
        let second  = (digits[1] + digits[3] + digits[5] + digits[7])

        let digit10 = (first - second) % 10
        let digit11 = (digits[0] + digits[1] + digits[2] + digits[3] + digits[4] + digits[5] + digits[6] + digits[7] + digits[8] + digits[9]) % 10

        if (digits[10] == digit11) && (digits[9] == digit10)
        {
            return true
        }
    }
}
return false
}

我到处找了,但没有用土耳其语写。我想这就是我找不到的原因。非常感谢。不客气,顺便问一下,你可以查看我的gist文件。它比较短。是的,它很棒!非常感谢你!