In iOS, regular expressions are used to determine whether the ID card format and bank card number format are correct (recommended). ios Regular Expressions

Source: Internet
Author: User
Tags modulus

In iOS, regular expressions are used to determine whether the ID card format and bank card number format are correct (recommended). ios Regular Expressions

1. Sometimes we need to upload the ID card number or bank card number. In this case, we need to make a basic judgment on the ID card number and bank card number.

The following is the ID card number. If YES is returned, the result is invalid.

# Pragma mark determines whether the ID card number is legal-(BOOL) judgeIdentityStringValid :( NSString *) identityString {if (identityString. length! = 18) return NO; // determine whether the basic ID card number meets the format NSString * regex2 = @ "^ (^ [1-9] \ d {7} (0 \ d) | (1 [0-2]) ([0 | 1 | 2] \ d) | 3 [0-1]) \ d {3} $) | (^ [1-9] \ d {5} [1-9] \ d {3} (0 \ d) | (1 [0-2]) ([0 | 1 | 2] \ d) | 3 [0-1]) (\ d {4 }) | \ d {3} [Xx]) $ "; NSPredicate * identityStringPredicate = [NSPredicate predicateWithFormat: @" self matches % @ ", regex2]; // if this verification succeeds, it indicates that the ID card format is correct, but the accuracy still needs to be calculated if (! [IdentityStringPredicate evaluateWithObject: identityString]) return NO; // ** Start verification * // Save the first 17 weighting factors in the array NSArray * idCardWiArray = @ [@ "7", @ "9 ", @ "10", @ "5", @ "8", @ "4", @ "2", @ "1", @ "6", @ "3 ", @ "7", @ "9", @ "10", @ "5", @ "8", @ "4", @ "2"]; // This is the possible 11-bit remainder and verification code after dividing by 11. It is also saved as an array NSArray * idCardYArray = @ [@ "1", @ "0 ", @ "10", @ "9", @ "8", @ "7", @ "6", @ "5", @ "4", @ "3 ", @ "2"]; // It is used to save the total NSInteger idCa of the first 17 digits following the weighting factor. RdWiSum = 0; for (int I = 0; I <17; I ++) {NSInteger subStrIndex = [[identityString substringWithRange: NSMakeRange (I, 1)] integerValue]; NSInteger idCardWiIndex = [[idCardWiArray objectAtIndex: I] integerValue]; idCardWiSum + = subStrIndex * idCardWiIndex;} // calculate the position of the array where the verification code is located. NSInteger idCardMod = idCardWiSum; // obtain the last ID number NSString * idCardLast = [identityString substringWithRange: NSMakeRange (17, 1)];/ /If it is equal to 2, the verification code is 10. the last digit of the ID card number should be X if (idCardMod = 2) {if (! [IdCardLast isEqualToString: @ "X"] | [idCardLast isEqualToString: @ "x"]) {return NO ;}} else {// The calculated verification code matches the last ID card number. if the verification code is the same, the verification code passes. Otherwise, the ID card number is invalid if (! [IdCardLast isEqualToString: [idCardYArray objectAtIndex: idCardMod]) {return NO ;}} return YES ;}

In the next step, the bank card number is determined to return "YES" or "true". Otherwise, it is invalid.

# Pragma mark determines whether the bank card number is legal-(BOOL) isBankCard :( NSString *) cardNumber {if (cardNumber. length = 0) {return NO;} NSString * digitsOnly = @ ""; char c; for (int I = 0; I <cardNumber. length; I ++) {c = [cardNumber characterAtIndex: I]; if (isdigit (c) {digitsOnly = [digitsOnly stringByAppendingFormat: @ "% c ", c] ;}} int sum = 0; int digit = 0; int addend = 0; BOOL timesTwo = false; for (NSInteger I = digitsOnly. len Vertex-1; I> = 0; I --) {digit = [digitsOnly characterAtIndex: I]-'0'; if (timesTwo) {addend = digit * 2; if (addend> 9) {addend-= 9 ;}} else {addend = digit;} sum + = addend; timesTwo =! TimesTwo;} int modulus = sum % 10; return modulus = 0 ;}

The above section describes how to use regular expressions in iOS to determine whether the ID card format and bank card number format are correct (recommended, if you have any questions, please leave a message and the editor will reply to you in time. Thank you very much for your support for the help House website!

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.