【问题标题】:Turkish Identity Number Verification in SwiftSwift 中的土耳其身份证号码验证
【发布时间】:2018-11-30 09:02:18
【问题描述】:

如何确保给定的文本是土耳其身份证号? 我见过js version herephthon version here

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

【问题讨论】:

    标签: swift function numeric digits turkish


    【解决方案1】:

    只是一个简单的实现:

    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 你可以看看我的要点,根据其他人来说它更短。

      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 这篇文章可能会对你有所帮助。

      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 文件。它更短。
      • 是的,很棒!非常感谢!
      【解决方案3】:

      通过使用guard,您可以摆脱所有这些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]
      }
      

      请注意,如果value 中的任何字符由于map 包含nil 的结果而无法转换为Int,则转换将如何失败。

      【讨论】:

        猜你喜欢
        • 2020-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-03
        • 1970-01-01
        相关资源
        最近更新 更多