ID card number verification, ID card number verification

Source: Internet
Author: User

ID card number verification, ID card number verification

Previously, I used regular expressions to verify the ID card number, but sometimes the wrong ID card number can also pass the front-end verification. Later I found that this method is good and I posted the code to you:

// ID card number verification + (BOOL) validateIdentityCard :( NSString *) cardNo {if (cardNo. length! = 18) {return NO;} NSArray * codeArray = [NSArray arrayWithObjects: @ "7", @ "9", @ "10", @ "5 ", @ "8", @ "4", @ "2", @ "1", @ "6", @ "3", @ "7", @ "9 ", @ "10", @ "5", @ "8", @ "4", @ "2", nil]; NSDictionary * checkCodeDic = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: @ "1", @ "0", @ "X", @ "9", @ "8", @ "7", @ "6", @ "5 ", @ "4", @ "3", @ "2", nil] forKeys: [NSArray arrayWithObjects: @ "0", @ "1", @ "2 ", @ "3", @ "4", @ "5", @ "6", @ "7", @ "8", @ "9", @ "1 0 ", nil]; ns1_* scan = [ns1_scannerwithstring: [cardNo substringToIndex: 17]; int val; BOOL isNum = [scan scanInt: & val] & [scan isAtEnd]; if (! IsNum) {return NO;} int sumValue = 0; for (int I = 0; I <17; I ++) {sumValue + = [[cardNo substringWithRange: NSMakeRange (I, 1)] intValue] * [[codeArray objectAtIndex: I] intValue];} NSString * strlast = [checkCodeDic objectForKey: [NSString stringWithFormat: @ "% d ", sumValue % 11]; if ([strlast islast tostring: [cardNo substringWithRange: NSMakeRange (17, 1)] uppercaseString]) {return YES;} return NO ;}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.