校验身份证是否有效
2019-07-01 本文已影响0人
好快的刀
let cardNo = "34052419800101001X"
extension String {
//校验身份证是否有效,校验最后一位
funccardNoEffective(_cardNo:String)->Bool{
ifcardNo.count!=18{
returnfalse
}
letjyArray: [Int] = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
letcard = cardNo[..
varnum =0
varindex =0
forcharincard{
ifletcharInt =Int(String(char)){
letjy = jyArray[index]
num += (charInt * jy)
}else{
returnfalse
}
index +=1
}
letmo = num %11
let xx = ["1","0","X","9","8","7","6","5","4","3","2"]
letmyLast = xx[mo]
letcardLast =String(cardNo.last!)
returnmyLast == cardLast
}
}