This time I will bring you the Luhn algorithm that matches the bank card number entered by the user. What are the precautions for using the Luhn algorithm that matches the bank card number entered by the user. The following is a practical case, let's take a look.
During development, sometimes, in order to create a better user experience and reduce the pressure on the server side, it is necessary to perform format verification on some such as mobile phone numbers, bank card numbers, and ID numbers
The following is the code to determine whether the bank card number input is correct (format verification based on Luhn algorithm):
iOS code:
/** * 银行卡格式校验 * * @param cardNo 银行卡号 * * @return */ + (BOOL) checkCardNo:(NSString*) cardNo{ int oddsum = 0; //奇数求和 int evensum = 0; //偶数求和 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 ; i>=1;i--) { NSString *tmpString = [cardNo substringWithRange:NSMakeRange(i-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:
/** * 匹配Luhn算法:可用于检测银行卡卡号 * @param cardNo * @return */ public static boolean matchLuhn(String cardNo) { int[] cardNoArr = new int[cardNo.length()]; for (int i=0; i<cardno.length>=0;i-=2) { cardNoArr[i] <p style="text-align: left;"><span style="color: #ff0000"><strong>Attached (bank card number formatting): </strong></span></p> <p style="text-align: left;">Enter the bank card number During the process, we usually insert a "-" every 4 digits. A format similar to "1332-2131-2313-1231-212"<br> can be achieved by the following method: </p> <p style="text-align: left;"><span style="background-color: #ccffcc"><strong>Set the proxy of textField</strong></span></p> <pre class="brush:php;toolbar:false">- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSString *text = [textField text]; NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"]; string = [string stringByReplacingOccurrencesOfString:@"-" withString:@""]; if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) { return NO; } text = [text stringByReplacingCharactersInRange:range withString:string]; text = [text stringByReplacingOccurrencesOfString:@"-" withString:@""]; NSString *newString = @""; while (text.length > 0) { NSString *subString = [text substringToIndex:MIN(text.length, 4)]; newString = [newString stringByAppendingString:subString]; if (subString.length == 4) { newString = [newString stringByAppendingString:@"-"]; } text = [text substringFromIndex:MIN(text.length, 4)]; } newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]]; if (newString.length >= 24) { return NO; } [textField setText:newString]; return NO; }
Finally, how to get the bank card number that does not contain the symbol "-"?
[self.textField.text stringByReplacingOccurrencesOfString:@"-" withString:@""]
I believe you have mastered the method after reading the case in this article, and there will be more exciting things. Please pay attention to other related articles on php Chinese website!
Recommended reading:
How to implement the fuzzy matching function of regular expressions
Regular expressions to verify whether the QQ number is entered correct
The above is the detailed content of Luhn algorithm for matching bank card numbers entered by users. For more information, please follow other related articles on the PHP Chinese website!

最近有很多win11遇到了输入体验对话框总是闪烁,关也关不掉的问题,这其实是由于win11的默认系统服务和组件导致的,我们需要先禁用相关服务,再禁用输入体验服务就可以解决了,下面一起来试试看吧。win11输入体验怎么关闭:第一步,右键开始菜单,打开“任务管理器”第二步,依次找到“CTF加载程序”、“MicrosoftIME”和“服务主机:Textinputmanagementservice”三个进程,右键“结束任务”第三步,打开开始菜单,在上方搜索并打开“服务”第四步,在其中找到“Textinp
![Windows输入遇到挂起或内存使用率高的问题[修复]](https://img.php.cn/upload/article/000/887/227/170835409686241.jpg)
Windows的输入体验是一个关键的系统服务,负责处理来自各种人机接口设备的用户输入。它在系统启动时自动启动,在后台运行。然而,有时候这个服务可能会出现自动挂起或占用过多内存的情况,导致系统性能下降。因此,及时监控和管理这个进程是至关重要的,以确保系统的效率和稳定性。在这篇文章中,我们将分享如何解决Windows输入体验被挂起或导致内存使用率高的问题。Windows输入体验服务没有用户界面,但它与处理与输入设备相关的基本系统任务和功能有密切关联。它的作用是帮助Windows系统理解用户输入的每一

两个微信不可以绑定同一张银行卡。绑定银行卡到微信账户操作:1、打开微信应用程序,点击“我”选项,然后选择“支付”选项;2、选择“添加银行卡”选项,并按照系统提示输入银行卡信息;3、一旦银行卡绑定成功,用户就可以在微信中使用该银行卡进行支付和转账。

别人的银行卡不可以绑定自己的微信。银行卡绑定微信的步骤:1、打开微信APP,初次登陆的需要输入的账号和密码;2、选择“我”,再选择“支付”;3、选择“钱包”;4、选择“银行卡”;5、选择“添加银行卡”;6、输入微信支付密码;7、输入需要绑定的银行卡号,再点击“下一步”,即可完成银行卡绑定。

1、打开微信app,进入【我】的页面,选择【服务】菜单,点击【钱包】。2、选择【银行卡】选项,在银行卡界面中选择需要解绑的银行卡,并进入其详情页。3、最后,点击右上角的三点图标,选择【解除绑定】并输入支付密码以完成解绑过程。

1、打开美团app,点击右下角的【我的】,在【钱包】的右侧点击【进入钱包】。2、选择【银行卡】选项,选择并点击需要解绑的银行卡,进入银行卡界面。3、点击【银行卡管理】,在弹出的窗口中选择【解除绑定】,点击【仍要解绑】。4、在身份验证界面输入支付密码,选择【解绑原因】或点击右上角的【跳过】即可。

1、打开微信app,点击右下角的【我】,选择【服务】选项,点击【钱包】。2、选择【银行卡】选项,在银行卡界面选择并点击想要解绑的银行卡,进入银行卡详情页。3、点击右上角的【三个点】图标,选择【解除绑定】,输入支付密码即可完成解绑操作。

1、打开支付宝app,进入【我的】页面,选择【银行卡】选项,点击目标银行卡。2、在银行卡界面,选择【更多功能】,进入【帮助中心】,勾选解绑原因后点击【去解绑】。3、最后,选择【仍要解绑】并输入支付密码,即可完成银行卡解绑。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
