这是我看到用 oc 写的身份证号的合法性校验后自己用 swift 写了一下,具体哪里看来的忘了,没标出处,望原作者见谅!
直接上代码!
func validateIDCardNumber(sfz:String)->Bool{
let value = sfz.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
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 as NSString).substringToIndex(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 as NSString).substringWithRange(NSRange(location:6,length:2)))!
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.numberOfMatchesInString(value, options:NSMatchingOptions.ReportProgress, range: NSMakeRange(0, value.characters.count))
if(numberofMatch > 0) {
return true
}else {
return false
}
case 18:
year = Int((value as NSString).substringWithRange(NSRange(location:6,length:4)))!
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.numberOfMatchesInString(value, options:NSMatchingOptions.ReportProgress, range: NSMakeRange(0, value.characters.count))
if(numberofMatch > 0) {
let s =
(Int((value as NSString).substringWithRange(NSRange(location:0,length:1)))! +
Int((value as NSString).substringWithRange(NSRange(location:10,length:1)))!) * 7 +
(Int((value as NSString).substringWithRange(NSRange(location:1,length:1)))! +
Int((value as NSString).substringWithRange(NSRange(location:11,length:1)))!) * 9 +
(Int((value as NSString).substringWithRange(NSRange(location:2,length:1)))! +
Int((value as NSString).substringWithRange(NSRange(location:12,length:1)))!) * 10 +
(Int((value as NSString).substringWithRange(NSRange(location:3,length:1)))! +
Int((value as NSString).substringWithRange(NSRange(location:13,length:1)))!) * 5 +
(Int((value as NSString).substringWithRange(NSRange(location:4,length:1)))! +
Int((value as NSString).substringWithRange(NSRange(location:14,length:1)))!) * 8 +
(Int((value as NSString).substringWithRange(NSRange(location:5,length:1)))! +
Int((value as NSString).substringWithRange(NSRange(location:15,length:1)))!) * 4 +
(Int((value as NSString).substringWithRange(NSRange(location:6,length:1)))! +
Int((value as NSString).substringWithRange(NSRange(location:16,length:1)))!) * 2 +
Int((value as NSString).substringWithRange(NSRange(location:7,length:1)))! * 1 +
Int((value as NSString).substringWithRange(NSRange(location:8,length:1)))! * 6 +
Int((value as NSString).substringWithRange(NSRange(location:9,length:1)))! * 3
let Y = s%11
var M = "F"
let JYM = "10X98765432"
M = (JYM as NSString).substringWithRange(NSRange(location:Y,length:1))
if M == (value as NSString).substringWithRange(NSRange(location:17,length:1))
{
return true
}else{return false}
}else {
return false
}
default:
return false
}
}