Verify whether the bank card number entered by the user is correct based on the format of Luhn algorithm
Editor to share with you based on the Luhn algorithm format to verify whether the bank card number entered by the user is correct, I hope you will gain something after reading this article, let's discuss it together!
IOS Code:
/ * * @ param cardNo bank card number * * @ return * / + (BOOL) checkCardNo: (NSString*) cardNo {int oddsum = 0; / / odd summation int evensum = 0; / / even summation int allsum = 0; int cardNoLength = (int) [cardNo length]; int lastNum = [[cardNo substringFromIndex:cardNoLength-1] intValue]; cardNo = [cardNo substringToIndex:cardNoLength-1] For (int I = cardNoLength-1)) {NSString * tmpString = [cardNo substringWithRange:NSMakeRange (imuri 1,1)]; int tmpVal = [tmpString intValue]; if (cardNoLength% 2 = = 1) {if ((I% 2) = = 0) {tmpVal * = 2; if (tmpVal > = 10) tmpVal-= 9; evensum + = tmpVal;} else {oddsum + = tmpVal }} else {if ((I% 2) = = 1) {tmpVal * = 2; if (tmpVal > = 10) tmpVal-= 9; evensum + = tmpVal;} else {oddsum + = tmpVal;}} allsum = oddsum + evensum; allsum + = lastNum; if ((allsum% 10) = = 0) return YES; else return NO;}
Androd Code:
/ * matching Luhn algorithm: it can be used to detect the bank card number * @ param cardNo * @ return * / public static boolean matchLuhn (String cardNo) {int [] cardNoArr = new int [cardNo.length ()]; for (int isidentin I = 0transferiMusure 2) {cardNoArr [I]