iOS身份證號碼匹配驗證

當用戶輸入完身份證號碼的時候我們通常需要確認其真實性,下面的代碼估計可以幫到您,試試看

// 匹配身份證號碼
-(BOOL)isHxIdentityCard
{
    // 判斷位數(shù)
    if ([self length] != 15 && [self length] != 18)
    {
        return NO;
    }
    
NSString *carid = self;
long lSumQT  =0;
// 加權因子
int R[] ={7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
// 校驗碼
unsigned char sChecker[11]={'1','0','X', '9', '8', '7', '6', '5', '4', '3', '2'};
// 將15位身份證號轉換成18位
NSMutableString *mString = [NSMutableString stringWithString:self];
if ([self length] == 15)
{
    [mString insertString:@"19" atIndex:6];
    long p = 0;
    const char *pid = [mString UTF8String];
    for (int i=0; i<=16; i++)
    {
        p += (pid[i]-48) * R[i];
    }
    int o = p%11;
    NSString *string_content = [NSString stringWithFormat:@"%c",sChecker[o]];
    [mString insertString:string_content atIndex:[mString length]];
    carid = mString;
}
// 判斷地區(qū)碼
NSString * sProvince = [carid substringToIndex:2];
if (![self areaCode:sProvince])
{
    return NO;
}
// 判斷年月日是否有效
// 年份
int strYear = [[carid substringWithRange:NSMakeRange(6,4)] intValue];
// 月份
int strMonth = [[carid substringWithRange:NSMakeRange(10,2)] intValue];
// 日
int strDay = [[carid substringWithRange:NSMakeRange(12,2)] intValue];

NSTimeZone *localZone = [NSTimeZone localTimeZone];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeZone:localZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date=[dateFormatter dateFromString:[NSString stringWithFormat:@"%d-%d-%d 12:01:01",strYear,strMonth,strDay]];
if (date == nil)
{
    return NO;
}
const char *PaperId  = [carid UTF8String];
// 檢驗長度
if( 18 != strlen(PaperId)) return -1;
// 校驗數(shù)字
for (int i=0; i<18; i++)
{
    if ( !isdigit(PaperId[i]) && !(('X' == PaperId[i] || 'x' == PaperId[i]) && 17 == i) )
    {
        return NO;
    }
}
// 驗證最末的校驗碼
for (int i=0; i<=16; i++)
{
    lSumQT += (PaperId[i]-48) * R[i];
}
if (sChecker[lSumQT%11] != PaperId[17] )
{
    return NO;
}
return YES;
}

-(BOOL)areaCode:(NSString *)code
{
  NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
  [dic setObject:@"北京" forKey:@"11"];
  [dic setObject:@"天津" forKey:@"12"];
  [dic setObject:@"河北" forKey:@"13"];
  [dic setObject:@"山西" forKey:@"14"];
[dic setObject:@"內蒙古" forKey:@"15"];
[dic setObject:@"遼寧" forKey:@"21"];
[dic setObject:@"吉林" forKey:@"22"];
[dic setObject:@"黑龍江" forKey:@"23"];
[dic setObject:@"上海" forKey:@"31"];
[dic setObject:@"江蘇" forKey:@"32"];
[dic setObject:@"浙江" forKey:@"33"];
[dic setObject:@"安徽" forKey:@"34"];
[dic setObject:@"福建" forKey:@"35"];
[dic setObject:@"江西" forKey:@"36"];
[dic setObject:@"山東" forKey:@"37"];
[dic setObject:@"河南" forKey:@"41"];
[dic setObject:@"湖北" forKey:@"42"];
[dic setObject:@"湖南" forKey:@"43"];
[dic setObject:@"廣東" forKey:@"44"];
[dic setObject:@"廣西" forKey:@"45"];
[dic setObject:@"海南" forKey:@"46"];
[dic setObject:@"重慶" forKey:@"50"];
[dic setObject:@"四川" forKey:@"51"];
[dic setObject:@"貴州" forKey:@"52"];
[dic setObject:@"云南" forKey:@"53"];
[dic setObject:@"西藏" forKey:@"54"];
[dic setObject:@"陜西" forKey:@"61"];
[dic setObject:@"甘肅" forKey:@"62"];
[dic setObject:@"青海" forKey:@"63"];
[dic setObject:@"寧夏" forKey:@"64"];
[dic setObject:@"新疆" forKey:@"65"];
[dic setObject:@"臺灣" forKey:@"71"];
[dic setObject:@"香港" forKey:@"81"];
[dic setObject:@"澳門" forKey:@"82"];
[dic setObject:@"國外" forKey:@"91"];
if ([dic objectForKey:code] == nil) {
    return NO;
}
return YES;
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 當用戶輸入完身份證號碼的時候我們通常需要確認其真實性,下面的代碼估計可以幫到您,試試看
    以德扶人閱讀 730評論 0 51
  • 轉自:http://www.itdecent.cn/p/ac4c4536ca3e# 一、前言??身份證識別,又稱O...
    ZhangCc_閱讀 1,627評論 1 11
  • 大山啊,我好羨慕你 你可以安靜地坐在那里 什么也不用想 看著日月變換,滄海桑田 河流啊,我好羨慕你 你從來不問自己...
    秋月醉閱讀 431評論 0 9
  • 昨晚睡得晚,巫睡著了,開始呼嚕,我卻還沒睡著,每次這樣我就慘了,怎么也睡不著了,催眠神篇《春江花月夜》返了不知道多...
    綠百合閱讀 152評論 0 0
  • 網絡素養(yǎng)1 網絡素養(yǎng)整本書主要圍繞了注意力,垃圾識別,參與,集體智慧,社會形狀這幾個關鍵詞來引導我們如何更好的使用...
    Innogen99閱讀 171評論 0 0

友情鏈接更多精彩內容