extension String {
//验证身份证
func validateIDCardNumber() -> Bool {
if self.characters.count != 18 {
return false
}
let codeArray = ["7","9","10","5","8","4","2","1","6","3","7","9","10","5","8","4","2"]
let checkCodeDic = NSDictionary(objects: ["1","0","X","9","8","7","6","5","4","3","2"], forKeys: ["0","1","2","3","4","5","6","7","8","9","10"])
let scan = NSScanner(string: self.substringToIndex(self.startIndex.advancedBy(17)))
var val:Int32 = 0
let isNum = scan.scanInt(&val) && scan.atEnd
if !isNum {
return false
}
var sumValue = 0
for i in 0 ..< 17 {
sumValue += Int(self.substringWithRange(Range(start: self.startIndex.advancedBy(i),end: self.startIndex.advancedBy(i+1))))! * Int(codeArray[i])!
}
let obj = checkCodeDic.objectForKey("\(sumValue%11)")
if obj == nil {
return false
}else {
if String(obj!) == self.substringWithRange(Range(start: self.startIndex.advancedBy(17),end: self.startIndex.advancedBy(18))).uppercaseString {
return true
}
}
return false
}
}