iOS验证判断身份证号的格式

来源:深圳沃龙软件开发公司     2017-06-16

项目中遇到了实名验证的需求,但没有和公安部的公民身份信息系统对接验证。严谨起见,就需要我们按照《公民身份号码》GB11643-1999,进行格式的验证。
18位身份证标准在国家质量技术监督局于1999年7月1日实施的GB11643-1999《公民身份号码》中做了明确的规定。GB11643-1999《公民身份号码》为GB11643-1989《社会保障号码》的修订版,其中指出将原标准名称“社会保障号码”更名为“公民身份号码”,另外GB11643-1999《公民身份号码》从实施之日起代替GB11643-1989


GB11643-1999

长度的判断

标准第5.1条 号码的结构 :公民身份号码是特征组合码,由十七位数字本体码(master number)和一位数字校验码(check number)组成。排序顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码。


号码的表示形式

- (BOOL)validateIdNumberLength:(NSString *)idNumber {//idNumber为传入的身份证号
    NSString *idNumberRegex = @"^(\\d{17})(\\d|[xX])$";//正则判断idNumber是17位数字加1位数字校验码或大小写xX
    NSPredicate *idNumberPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",idNumberRegex];    
    if(![idNumberPredicate evaluateWithObject:idNumber]){        
        NSLog(@"你输入的身份证长度或格式错误");        
        return NO;
    } else {        
        return YES;
    }
}

地址码的判断

地址码:表示编码对象常住户口所在县(市、旗、区)的行政区划代码,按GB/T2260的规定执行。
大陆居民身份证号码中的地址码的数字编码规则为:
第一、二位表示省(自治区、直辖市、特别行政区)。
第三、四位表示市(地级市、自治州、盟及国家直辖市所属市辖区和县的汇总码)。其中,01-20,51-70表示省直辖市;21-50表示地区(自治州、盟)。
第五、六位表示县(市辖区、县级市、旗)。01-18表示市辖区或地区(自治州、盟)辖县级市;21-80表示县(旗);81-99表示省直辖县级市。 

- (BOOL)validateIdNumberProvince:(NSString *)idNumber {//只判断了地址码的前两位省份 idNumber为传入的身份证号
    NSDictionary *aProvince = @{@"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 *key = [idNumber substringToIndex:2];    
    NSString *value = [aProvince objectForKey:key];    
    if(!value){        
        NSLog(@"你的身份证地区非法");        
        return NO;
    } else {       
        return YES;
    }
}

出生日期码的判断

出生日期码:表示编码对象出生的年、月、日,按GB/T7408的规定执行,年、月、日分别用4位、2位、2位数字表示,之间不用分隔符。

- (BOOL)validateIdNumberDateOfBirth:(NSString *)idNumber {//idNumber为传入的身份证号
    NSString *birth = [idNumber substringWithRange:NSMakeRange(6, 8)];   
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];//创建一个日期格式化器
    dateFormatter.dateFormat=@"yyyyMMdd";   
    NSDate *date = [dateFormatter dateFromString:birth];    
    if(!date){        
        NSLog(@"身份证上的出生日期非法");        
        return NO;
    } else {        
        return YES;
    }
}

校验码的判断

校验码:附加在本体码后边 ,用来验证本体码的录入或转录过程准确性的号码 。每一个本体码只有一个校验码 ,校验码通过规定的数学关系式得到。


校验公式

- (BOOL)validateIdNumberCheckNumber:(NSString *)idNumber {//idNumber为传入的身份证号
    NSArray *idCardWi = @[@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2"]; //将前17位加权因子保存在数组里
    NSArray *idCardA1 = @[@"1", @"0", @"10", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2"]; //这是除以11后,可能产生的11位余数、验证码,也保存成数组
    int idCardWiSum = 0; //用来保存前17位各自乖以加权因子后的总和
    for (int i = 0; i < 17; i++) {
        idCardWiSum += [[idNumber substringWithRange:NSMakeRange(i,1)] intValue]*[idCardWi[i] intValue];
    }    
    int idCardMod = idCardWiSum%11;//计算出校验码所在数组的位置
    NSString *idCardLast = [idNumber substringWithRange:NSMakeRange(17,1)];//得到最后一位身份证号码
    //如果等于2,则说明校验码是10,身份证号码最后一位应该是X
    if (idCardMod == 2) {        
        if ([idCardLast isEqualToString:@"X"] || [idCardLast isEqualToString:@"x"]) {            
            return YES;
        } else {            
            NSLog(@"你输入的身份证号非法");            
            return NO;
        }
    } else {        //用计算出的验证码与最后一位身份证号码匹配,如果一致,说明通过,否则是无效的身份证号码
        if ([idCardLast intValue] == [idCardA1[idCardMod] intValue]) {            
            return YES;
        } else {            
            NSLog(@"你输入的身份证号非法");            
            return NO;
        }
    }
}

最后关于顺序码

顺序码:表示在同一地址码所标识 的区域范围内,对同年、同月、同日出生的人编定的顺序号 ,顺序码的奇数分配给男性 ,偶数分配给女性 。如:007的就是个男生 而且和他同年月日生的男生至少有两个 他们的后四位是001 和 003 。


本内容皆为 深圳沃龙软件开发公司 原创
如需转载,请注明文章出处和来源网址:http://www.wooolong.com/blog/17.html

全部评论

ThomasLop | 2024-07-17

Came across an interesting article, I propose you have a look 《a href=HHtps://www.bluebirdbanter.com/users/worksale555》HHtps://www.bluebirdbanter.com/users/worksale555《/a》

Reply

Jamesvem | 2024-07-17

Found an article that's definitely worth your time Ц take a look 《a href=HHtps://www.article1.co.uk/Articles-of-2024-Europe-UK-US/цифровой-аромат-как-ароматерапи¤-может-вли¤ть-на-ваше-онлайн-общение》HHtps://www.article1.co.uk/Articles-of-2024-Europe-UK-US/цифровой-аромат-как-ароматерапи¤-может-вли¤ть-на-ваше-онлайн-общение《/a》

Reply

Michealpau | 2024-07-29

Came across an intriguing article – it's worth your attention, trust me ?a href=HHtps://www.blogtalkradio.com/degilciqcg?HHtps://www.blogtalkradio.com/degilciqcg?/a?

Reply

June | 2024-07-29

I like the helpful information you provide in your articles. I will bookmark your weblog and check again here frequently. I am quite sure I will learn lots of new stuff right here! Best of luck for the next!

Reply

ScottSmork | 2024-07-30

Found captivating reading that I'd like to offer you – you won't regret it ?a href=HHtp://abroad.ekafe.ru/viewtopic.php?f=5& t=1984?HHtp://abroad.ekafe.ru/viewtopic.php?f=5& t=1984?/a?

Reply

ScottSmork | 2024-07-30

Stumbled upon an interesting article – I suggest you take a look ?a href=HHtp://moningrp.listbb.ru/viewtopic.php?f=16& t=479?HHtp://moningrp.listbb.ru/viewtopic.php?f=16& t=479?/a?

Reply

Timothyelu | 2024-07-31

Came across an interesting article, I propose you have a look ?a href=HHtps://imperialtechno.ru/elitnyj-eskort-v-gorode-moskva-polzuetsya-bolshim-sprosom-blagodarya-vysokogo-urovnya-obsluzhivaniya-i-utonchennosti-kotoruyu-predostavlyayut-agenty/?HHtps://imperialtechno.ru/elitnyj-eskort-v-gorode-moskva-polzuetsya-bolshim-sprosom-blagodarya-vysokogo-urovnya-obsluzhivaniya-i-utonchennosti-kotoruyu-predostavlyayut-agenty/?/a?

Reply

KennethTru | 2024-08-01

Came across an interesting article, I propose you have a look ?a href=HHtp://www.bausch.com.ph/en/redirect/?url=HHtps://www.empowher.com/user/4346664?HHtp://www.bausch.com.ph/en/redirect/?url=HHtps://www.empowher.com/user/4346664?/a?

Reply

Katrin | 2024-08-02

I love your blog.. very nice colors & theme. Did you make this website yourself or did you hire someone to do it for you? Plz reply as I'm looking to create my own blog and would like to know where u got this from. kudos

Reply

DavidHem | 2024-08-02

Found a captivating read that I'd like to recommend to you ?a href=HHtp://lozd.com/index.php?url=HHtps://www.mediafire.com/file/6u9rmux8ymreewz/pdf-50352-54199.pdf/file?HHtp://lozd.com/index.php?url=HHtps://www.mediafire.com/file/6u9rmux8ymreewz/pdf-50352-54199.pdf/file?/a?

Reply

JekDon | 2024-08-03

HHtps://r7casino-9j.ru Start winning today, get a no deposit gift or free spins of your choice. Only the top best bonuses!

Reply

RichardMut | 2024-08-03

Encountered a unique article – be sure to take a look and see for yourself ?a href=HHtp://mirstalkera.4admins.ru/viewtopic.php?f=50& t=2162?HHtp://mirstalkera.4admins.ru/viewtopic.php?f=50& t=2162?/a?

Reply

Georgeper | 2024-08-05

Discovered an article that might catch your interest – don't miss it! ?a href=HHtps://pixabay.com/users/45173592/?HHtps://pixabay.com/users/45173592/?/a?

Reply

Wilson | 2024-08-05

Thanks for every other fantastic post. Where else may anyone get that kind of info in such a perfect approach of writing? I have a presentation subsequent week, and I am on the search for such information.

Reply

Frankethek | 2024-08-06

Found captivating reading that I'd like to offer you – you won't regret it ?a href=HHtp://daparamall.astroweb.co.kr/bbs/board.php?bo_table=free& wr_id=292934?HHtp://daparamall.astroweb.co.kr/bbs/board.php?bo_table=free& wr_id=292934?/a?

Reply

DonovanHEX | 2024-08-07

Found captivating reading that I'd like to recommend to everyone ?a href=HHtp://kvitka.ukrbb.net/viewtopic.php?f=58& t=24212?HHtp://kvitka.ukrbb.net/viewtopic.php?f=58& t=24212?/a?

Reply

RicardoCop | 2024-08-07

Encountered a unique article – be sure to take a look and see for yourself ?a href=HHtp://sadcr.listbb.ru/viewtopic.php?f=53& t=440?HHtp://sadcr.listbb.ru/viewtopic.php?f=53& t=440?/a?

Reply

JeromeTut | 2024-08-08

Found an article that's definitely worth your time – take a look ?a href=HHtp://pirat.iboards.ru/viewtopic.php?f=20& t=23129?HHtp://pirat.iboards.ru/viewtopic.php?f=20& t=23129?/a?

Reply

AndreRES | 2024-08-09

Discovered an article that's sure to appeal to you – I recommend checking it out ?a href=HHtp://spiderproject.com.ua/bitrix/rk.php?goto=HHtps://kniganasluh.com/user/teigetaxka?HHtp://spiderproject.com.ua/bitrix/rk.php?goto=HHtps://kniganasluh.com/user/teigetaxka?/a?

Reply

HarrySag | 2024-08-10

Opened up an enthralling read – I'd like to share it with you ?a href=HHtp://forum54.4adm.ru/viewtopic.php?f=69& t=8573?HHtp://forum54.4adm.ru/viewtopic.php?f=69& t=8573?/a?

Reply

AnthonyBai | 2024-08-11

Came across an interesting article, worth a glance ?a href=HHtp://student-news.ru/filmyi-i-serialyi-onlayn-premeryi-sezona?HHtp://student-news.ru/filmyi-i-serialyi-onlayn-premeryi-sezona?/a?

Reply

DanielSkyn | 2024-08-13

Found an enthralling read that I'd recommend – it's truly fascinating ?a href=HHtp://kolba.com.ua/index.php?topic=141747.new#new?HHtp://kolba.com.ua/index.php?topic=141747.new#new?/a?

Reply

JekDon | 2024-08-13

HHtps://azino777ofitsialnii.ru Start winning today, get a no deposit gift or free spins of your choice. Only the top best bonuses!

Reply

JekDon | 2024-08-14

HHtp://dns2.shadr.ru/user/alekzblelp/ Start winning today, get a no deposit gift or free spins of your choice. Only the top best bonuses!

Reply

Christi | 2024-08-14

Thank you for sharing your info. I really appreciate your efforts and I will be waiting for your further post thank you once again.

Reply

Felipe | 2024-08-15

Hey there I am so delighted I found your webpage, I really found you by error, while I was browsing on Yahoo for something else, Anyhow I am here now and would just like to say thanks a lot for a fantastic post and a all round entertaining blog (I also love the theme/design), I don't have time to go through it all at the minute but I have saved it and also included your RSS feeds, so when I have time I will be back to read more, Please do keep up the excellent work.

Reply

Gilberto | 2024-08-15

I used to be able to find good information from your articles.

Reply

Matt | 2024-08-15

naturally like your web-site but you need to test the spelling on several of your posts. Several of them are rife with spelling issues and I to find it very bothersome to tell the truth nevertheless I'll surely come back again.

Reply

Robertexcu | 2024-08-15

Found captivating reading that I'd like to recommend to everyone ?a href=HHtps://rt.rulet-18.com/?HHtps://rt.rulet-18.com?/a?

Reply

Jessetrund | 2024-08-18

?a href=HHtps://arendakatera.pro/vladivostok/jet-ski/??????? ?????????? ????????????/a?. ??????? ????? ??????????? ? ?????? ?? ????????????. ?? ??????? arendakatera.pro ?? ?????? ????? ??? ????? ? ?????? ????????? ????? ????? ---????a href=HHtps://arendakatera.pro/vladivostok/jet-ski/??????? ?????????? ????????????/a?

Reply

Wilma | 2024-08-21

?????? ????. ?????? ?????? ???????????? ???????? ?? ????? ????. ?????? ???????? ? ??????? ??????????,. ?????, ????, ?????? ?? ?????????? ?? ???????????? ??????. ???? arendakatera.pro" ??????? ????????. ??????? ????? ??????? ????????. ?????????? ????????. ???? ??? ???? ?????? ?

Reply

GregoryTha | 2024-08-24

Came across an intriguing article – it's worth your attention, trust me ?a href=HHtps://ruera.ru/interesno/6482-realnye-individualki-kieva.html?HHtps://ruera.ru/interesno/6482-realnye-individualki-kieva.html?/a?

Reply

Maddison | 2024-09-08

?????? ?????? ??????????? . ?????? ?????? ???????????? ???????? ?? ????? ????. ?????? ???????? ? ??????? ??????????,. ?????, ????, ?????? ?? ?????????? ?? ???????????? ??????. ???? arendakatera.pro" ??????? ????????. ??????? ????? ??????? ????????. ?????????? ????????. ??? ?

Reply

Maddison | 2024-09-08

?????? ?????? ??????????? . ?????? ?????? ???????????? ???????? ?? ????? ????. ?????? ???????? ? ??????? ??????????,. ?????, ????, ?????? ?? ?????????? ?? ???????????? ??????. ???? arendakatera.pro" ??????? ????????. ??????? ????? ??????? ????????. ?????????? ????????. ??? ?

Reply

Earlene | 2024-09-09

I have to thank you for the efforts you have put in writing this website. I'm hoping to see the same high-grade content by you later on as well. In truth, your creative writing abilities has motivated me to get my own site now ;)

Reply

Damien | 2024-09-09

HD Porno Tube ?????????? ????????? ???? ??????????, ????? ??? ?????? (??????), ??????????? (??????, ?????????), ????????????? (???, ????????), ??????????? (???????????), ? ??????????? (?????????).

Reply

Larryflola | 2024-09-10

Opened up interesting material – I recommend sharing this discovery ?a href=HHtp://dog-ola.ru/viewtopic.php?f=28& t=7907?HHtp://dog-ola.ru/viewtopic.php?f=28& t=7907?/a?

Reply

CharlesOff | 2024-09-10

Found captivating reading that I'd like to recommend to everyone ?a href=HHtp://psicolinguistica.letras.ufmg.br/wiki/index.php/15_Best_Zufalliger_Video-Chat_Bloggers_You_Need_to_Follow?HHtp://psicolinguistica.letras.ufmg.br/wiki/index.php/15_Best_Zufalliger_Video-Chat_Bloggers_You_Need_to_Follow?/a?

Reply

MartyHag | 2024-09-11

Encountered a captivating article, I propose you read ?a href=HHtp://veniaminv.flybb.ru/viewtopic.php?f=1& t=3212?HHtp://veniaminv.flybb.ru/viewtopic.php?f=1& t=3212?/a?

Reply

ThomasPier | 2024-09-11

Opened up an intriguing read – let me share this with you ?a href=HHtp://forum.drustvogil-galad.si/index.php/board,2.0.html?HHtp://forum.drustvogil-galad.si/index.php/board,2.0.html?/a?

Reply

Kevinjek | 2024-09-12

Came across an interesting article, I propose you have a look ?a href=HHtp://skazka.g-talk.ru/viewtopic.php?f=1& t=1979?HHtp://skazka.g-talk.ru/viewtopic.php?f=1& t=1979?/a?

Reply

CharlesToP | 2024-09-12

Found an enthralling read that I'd recommend – it's truly fascinating ?a href=HHtp://bahchisaray.org.ua/index.php?act=post& do=new_post& f=2?HHtp://bahchisaray.org.ua/index.php?act=post& do=new_post& f=2?/a?

Reply

elommafum | 2024-09-12

Benzo a pyrene is considered a classic DNA damaging carcinogen and is one of a multitude of polycyclic aromatic hydrocarbons commonly found in tobacco smoke and in the ambient environment ?a href=HHtps://enhanceyourlife.mom/?priligy dapoxetine review?/a? NOW is a popular supplement brand, and this particular supplement is designed Signs Of Low Blood Sugar too much sugar diabetes to support your blood sugar and metabolic rate

Reply

Jacobestax | 2024-09-13

Came across a unique article – it's worth your attention ?a href=HHtp://w77515cs.beget.tech/2024/09/07/evropeyski-avtomobili-z-dostavkoyu-bez-prihovanih-vitrat.html?HHtp://w77515cs.beget.tech/2024/09/07/evropeyski-avtomobili-z-dostavkoyu-bez-prihovanih-vitrat.html?/a?

Reply

发表评论