Turkish Identity Number Verification in Swift

你。 提交于 2019-12-06 11:24:01

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
}

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
}

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.

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