Turkish Identity Number Verification in Swift

那年仲夏 提交于 2019-12-07 22:41:16

问题


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

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

10th digit must be,

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

11th must be,

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

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


回答1:


Just a simple implementation:

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
}



回答2:


https://gist.github.com/befy/91dbdb9239fbf726cc1eaeeb5d9d6151 you can check out my gist, it's shorter according to the others.

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/

https://medium.com/@ntanyeri/swift-ile-tc-numaras%C4%B1-do%C4%9Frulama-24c7a9827ed This post might be help you.

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
}



回答3:


By using guard you can get rid of the deep nesting of all these ifs:

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]
}

Note how on line 4 the cast will fail if any of the characters in value is not convertible to an Int due to the result of map containing nil.



来源:https://stackoverflow.com/questions/53554304/turkish-identity-number-verification-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!