iOS 身份证正则表达式
程序员文章站
2022-04-14 11:00:52
ios 身份证正则表达式。
/*
校验身份证号码
*/
+ (bool)validateidcardnumber:(nsstring *)value {
value = [value...
ios 身份证正则表达式。
/* 校验身份证号码 */ + (bool)validateidcardnumber:(nsstring *)value { value = [value stringbytrimmingcharactersinset:[nscharacterset whitespaceandnewlinecharacterset]]; long length =0; if (!value) { return no; }else { length = 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 false; } 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; } break; 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[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[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)]]) { return yes;// 检测id的校验位 }else { return no; } }else { return no; } break; default: return no; break; } }
上一篇: 百度指数创建新词涨价到198元/年