1. Sometimes we use the upload ID number, or the bank card number, this time we need to the ID card number and the bank card number, the basic judgment.
The following is the judgement of the ID number return Yes is legal, otherwise illegal
#pragma mark to determine if the ID number is legal-(BOOL) Judgeidentitystringvalid: (NSString *) identitystring {if (identitystring.length!= 18)
return NO; Regular expressions determine if the base ID number satisfies 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 you pass this verification, the ID card format is correct, but the accuracy still needs to calculate if (![
Identitystringpredicate evaluatewithobject:identitystring]) return NO; * * Start validation *////Save the first 17-bit weighting factor 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 11-bit remainder, the verification code, which is divided by 11, also save an array of Nsarray *idcardyarray = @[@ "1", @ "0", @ "10", @ "9", @ "8", @ "7", @ "6", @ "5", @ "4", @ "3", @
"2"];
Used to preserve the sum of the first 17 Nsinteger idcardwisum = 0 after the weighted factor; 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 location of the array where the checksum code is located nsinteger idcardmod=idcardwisum%11;
Get the last ID number nsstring *idcardlast= [identitystring substringwithrange:nsmakerange (17, 1)]; If equal to 2, then the checksum code is 10, the last one of the ID number should be x if (idcardmod==2) {if (![ Idcardlast isequaltostring:@ "X"]| | [Idcardlast isequaltostring:@ "X"])
{return NO; }else{///With the calculated verification code to match the last ID number, if consistent, the description passed, otherwise it is invalid ID number if (![
Idcardlast isequaltostring: [Idcardyarray Objectatindex:idcardmod]]) {return NO;
} return YES; }
In the following is the judgment of the bank card number returned Yes or is really legal, otherwise illegal
#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.length-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%;
return modulus = = 0;
}
The above is a small set of iOS to introduce the use of regular expressions to determine the identity card format and bank card number format is correct (recommended), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!