美文网首页
iOS-swift3.0 点滴积累:身份证号码校验

iOS-swift3.0 点滴积累:身份证号码校验

作者: xiaopavip | 来源:发表于2017-07-08 09:55 被阅读0次

身份证号码校验,server端和app端都可以实现。下面实现swift3.0 校验身份证号码是否合法。用swfit 3.0 的String字符串截取

Swift 3的String有三个方法用于做字符串截取

tr.substring(to: String.Index)
str.substring(from: String.Index)
str.substring(with: Range<String.Index>)

用法示例:

var str = "Hello, World"

//这个方法会从字符串的开始截取到to参数指定的索引
str.substring(to: String.Index)

let index = str.index(str.startIndex, offsetBy: 5)  //索引为从开始偏移5个位置
str.substring(to: index)  // 获取Hello

//这个方法会从from参数指定的索引截取到字符串的末尾
substring(from: String.Index)

let index = str.index(str.startIndex, offsetBy: 7) //索引从开始偏移7个位置
str.substring(from: index)  // 输出World

//这个方法是截取指定的字符串范围,范围由Range指定
str.substring(with: Range<String.Index>)

let start = str.index(str.startIndex, offsetBy: 7)  //索引从开始偏移7个位置
let end = str.index(str.endIndex, offsetBy: -3)   //所有从末尾往回偏移三个位置
let range = start..<end

str.substring(with: range)  // 输出Wo

校验身份证号码是否合法:

func validateIDCardNumber(sfz: String) -> Bool {
        let value = sfz.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines)
        var length = 0
        if value == "" {
            return false
        } else {
            length = value.characters.count
            if length != 15 && length != 18 {
                return false
            }
        }
        
        //省份代码
        let arearsArray = ["11","12", "13", "14",  "15", "21",  "22", "23",  "31", "32",  "33", "34",  "35", "36",  "37", "41",  "42", "43",  "44", "45",  "46", "50",  "51", "52",  "53", "54",  "61", "62",  "63", "64",  "65", "71",  "81", "82",  "91"]
        let valueStart2 = value.substring(to: value.index(value.startIndex, offsetBy: 2))
        var arareFlag = false
        if arearsArray.contains(valueStart2) {
            arareFlag = true
        }
        if !arareFlag {
            return false
        }
        var regularExpression = NSRegularExpression()
        
        var numberofMatch = Int()
        var year = 0
        switch (length) {
        case 15:
            year = Int(value.substring(with: value.index(value.startIndex, offsetBy: 5)..<value.index(value.startIndex, offsetBy: 9)))!
            if year%4 == 0 || (year%100 == 0 && year%4 == 0) {
                do {
                    regularExpression = try NSRegularExpression.init(pattern: "^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$", options: .caseInsensitive) //检测出生日期的合法性
                } catch { }
            } else {
                do {
                    regularExpression =  try NSRegularExpression.init(pattern: "^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$", options: .caseInsensitive) //检测出生日期的合法性
                    
                } catch { }
            }
            
            numberofMatch = regularExpression.numberOfMatches(in: value, options:NSRegularExpression.MatchingOptions.reportProgress, range: NSMakeRange(0, value.characters.count))
            
            if (numberofMatch > 0) {
                return true
            } else {
                return false
            }
            
        case 18:
            year = Int(value.substring(with: value.index(value.startIndex, offsetBy: 6)..<value.index(value.startIndex, offsetBy: 10)))!
            if year%4 == 0 || (year%100 == 0 && year%4 == 0) {
                do {
                    regularExpression = try NSRegularExpression.init(pattern: "^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$", options: .caseInsensitive) //检测出生日期的合法性
                } catch { }
            } else {
                do {
                    regularExpression =  try NSRegularExpression.init(pattern: "^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$", options: .caseInsensitive) //检测出生日期的合法性
                    
                } catch { }
            }
            
            numberofMatch = regularExpression.numberOfMatches(in: value, options:NSRegularExpression.MatchingOptions.reportProgress, range: NSMakeRange(0, value.characters.count))
            
            if(numberofMatch > 0) {
                
                let s =
                    (Int(value.substring(with: value.startIndex..<value.index(value.startIndex, offsetBy: 1)))! +
                        Int(value.substring(with: value.index(value.startIndex, offsetBy: 10)..<value.index(value.startIndex, offsetBy: 11)))!) * 7 +
                        (Int(value.substring(with: value.index(value.startIndex, offsetBy: 1)..<value.index(value.startIndex, offsetBy: 2)))! +
                            Int(value.substring(with: value.index(value.startIndex, offsetBy: 11)..<value.index(value.startIndex, offsetBy: 12)))!) * 9 +
                        (Int(value.substring(with: value.index(value.startIndex, offsetBy: 2)..<value.index(value.startIndex, offsetBy: 3)))! +
                            Int(value.substring(with: value.index(value.startIndex, offsetBy: 12)..<value.index(value.startIndex, offsetBy: 13)))!) * 10 +
                        (Int(value.substring(with: value.index(value.startIndex, offsetBy: 3)..<value.index(value.startIndex, offsetBy: 4)))! +
                            Int(value.substring(with: value.index(value.startIndex, offsetBy: 13)..<value.index(value.startIndex, offsetBy: 14)))!) * 5 +
                        (Int(value.substring(with: value.index(value.startIndex, offsetBy: 4)..<value.index(value.startIndex, offsetBy: 5)))! +
                            Int(value.substring(with: value.index(value.startIndex, offsetBy: 14)..<value.index(value.startIndex, offsetBy: 15)))!) * 8 +
                        (Int(value.substring(with: value.index(value.startIndex, offsetBy: 5)..<value.index(value.startIndex, offsetBy: 6)))! +
                            Int(value.substring(with: value.index(value.startIndex, offsetBy: 15)..<value.index(value.startIndex, offsetBy: 16)))!) * 4 +
                        (Int(value.substring(with: value.index(value.startIndex, offsetBy: 6)..<value.index(value.startIndex, offsetBy: 7)))! +
                            Int(value.substring(with: value.index(value.startIndex, offsetBy: 16)..<value.index(value.startIndex, offsetBy: 17)))!) *  2 +
                        Int(value.substring(with: value.index(value.startIndex, offsetBy: 7)..<value.index(value.startIndex, offsetBy: 8)))! * 1 +
                        Int(value.substring(with: value.index(value.startIndex, offsetBy: 8)..<value.index(value.startIndex, offsetBy: 9)))! * 6 +
                        Int(value.substring(with: value.index(value.startIndex, offsetBy: 9)..<value.index(value.startIndex, offsetBy: 10)))! * 3
                
                let Y = s%11
                var M = "F"
                let JYM = "10X98765432"
                M = JYM.substring(with: JYM.index(JYM.startIndex, offsetBy: Y)..<JYM.index(JYM.startIndex, offsetBy: Y + 1))
                if M == value.substring(with: value.index(value.startIndex, offsetBy: 17)..<value.index(value.startIndex, offsetBy: 18)) {
                    return true
                } else {
                    return false
                }
            } else {
                return false
            }
        default:
            return false
        }
        
    }

相关文章

网友评论

      本文标题:iOS-swift3.0 点滴积累:身份证号码校验

      本文链接:https://www.haomeiwen.com/subject/nuvlhxtx.html