Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Verify whether the bank card number entered by the user is correct based on the format of Luhn algorithm

Shulou Source: shulou.com Published: 2022-06-02 03:59:57 09月26日 Update

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]

Tags: Bank card number format algorithm input user code article bank card even number can be passed odd done method more knowledge symbol industry information information channel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Tech Info MariaDB Microsoft Redmi