国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

Objective-C實現身份證驗證的方法示例

2019-10-21 18:47:36
字體:
來源:轉載
供稿:網友

cms/diguocms/32737.html">color: #ff0000">前言

最近在一個二次開發的項目中看到了一段身份證驗證的OC代碼,雖然我一直討厭二次開發。因為這這個二次開發的項目太老,代碼太亂,毫無層次感??墒橇钊诵牢康氖?,我在里面發現了一段有用的代碼,感興趣的可以參考學習。

直接上代碼

- (BOOL)isValidIdCardNum{ NSString *value = [self copy]; value = [value stringByReplacingOccurrencesOfString:@"X" withString:@"x"]; value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; int length = 0; if (!value) {  return NO; }else {  length = (int)value.length;  if (length != 15 && length !=18) {   return NO;  } } // 省份代碼 NSArray *areasArray =@[@"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"]; NSString *valueStart2 = [value substringToIndex:2]; BOOL areaFlag = NO; for (NSString *areaCode in areasArray) {  if ([areaCode isEqualToString:valueStart2]) {   areaFlag = YES;   break;  } } if (!areaFlag) {  return NO; } NSRegularExpression *regularExpression; NSUInteger numberofMatch; int year = 0; switch (length) {  case 15:   year = [value substringWithRange:NSMakeRange(6,2)].intValue +1900;   if (year % 4 ==0 || (year % 100 ==0 && year % 4 ==0)) {    regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[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:NSRegularExpressionCaseInsensitive error:nil];// 測試出生日期的合法性   }else {    regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[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:NSRegularExpressionCaseInsensitive error:nil];// 測試出生日期的合法性   }   numberofMatch = [regularExpression numberOfMatchesInString:value options:NSMatchingReportProgress range:NSMakeRange(0, value.length)];   if(numberofMatch > 0) {    return YES;   }else {    return NO;   }  case 18:   year = [value substringWithRange:NSMakeRange(6,4)].intValue;   if (year % 4 ==0 || (year % 100 ==0 && year % 4 ==0)) {    regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}19|20[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:NSRegularExpressionCaseInsensitive error:nil];// 測試出生日期的合法性       }else {    regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}19|20[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:NSRegularExpressionCaseInsensitive error:nil];// 測試出生日期的合法性   }   numberofMatch = [regularExpression numberOfMatchesInString:value options:NSMatchingReportProgress range:NSMakeRange(0, value.length)];   if(numberofMatch > 0) {    int S = ([value substringWithRange:NSMakeRange(0,1)].intValue + [value substringWithRange:NSMakeRange(10,1)].intValue) *7 + ([value substringWithRange:NSMakeRange(1,1)].intValue + [value substringWithRange:NSMakeRange(11,1)].intValue) *9 + ([value substringWithRange:NSMakeRange(2,1)].intValue + [value substringWithRange:NSMakeRange(12,1)].intValue) *10 + ([value substringWithRange:NSMakeRange(3,1)].intValue + [value substringWithRange:NSMakeRange(13,1)].intValue) *5 + ([value substringWithRange:NSMakeRange(4,1)].intValue + [value substringWithRange:NSMakeRange(14,1)].intValue) *8 + ([value substringWithRange:NSMakeRange(5,1)].intValue + [value substringWithRange:NSMakeRange(15,1)].intValue) *4 + ([value substringWithRange:NSMakeRange(6,1)].intValue + [value substringWithRange:NSMakeRange(16,1)].intValue) *2 + [value substringWithRange:NSMakeRange(7,1)].intValue *1 + [value substringWithRange:NSMakeRange(8,1)].intValue *6 + [value substringWithRange:NSMakeRange(9,1)].intValue *3;    int Y = S % 11;    NSString *M = @"F";    NSString *JYM = @"10X98765432";    M = [JYM substringWithRange:NSMakeRange(Y,1)]; // 判斷校驗位    if ([M isEqualToString:[[value substringWithRange:NSMakeRange(17,1)] uppercaseString]]) {     return YES;// 檢測ID的校驗位    }else {     return NO;    }   }else {    return NO;   }     default:   return NO; } return NO;}

上面的代碼驗證十分全面。

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宝鸡市| 习水县| 沙洋县| 东城区| 大安市| 纳雍县| 和平县| 连江县| 双城市| 赤城县| 九江市| 孝昌县| 郁南县| 罗定市| 任丘市| 原阳县| 来凤县| 巴彦淖尔市| 延长县| 济源市| 商水县| 陵川县| 德保县| 阿拉善左旗| 九江县| 宁津县| 龙陵县| 乌恰县| 大洼县| 彭阳县| 新化县| 凤山县| 南雄市| 固原市| 浦北县| 福清市| 麻阳| 武平县| 香港 | 桦川县| 莲花县|