Home Web Front-end JS Tutorial Method to determine whether the bank card number entered by the user is correct_regular expression

Method to determine whether the bank card number entered by the user is correct_regular expression

Jun 28, 2017 pm 01:39 PM
enter bank

The following editor will bring you a method to determine whether the bank card number entered by the user is correct (format verification based on Luhn algorithm). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and 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 format some such as mobile phone numbers, bank card numbers, and ID numbers. Verification

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;

}
Copy after login

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(); i++) {

 

    cardNoArr[i] = Integer.valueOf(String.valueOf(cardNo.charAt(i)));

 

  }

 

  for(int i=cardNoArr.length-2;i>=0;i-=2) {

 

    cardNoArr[i] <<= 1;

 

    cardNoArr[i] = cardNoArr[i]/10 + cardNoArr[i]%10;

 

  }

 

  int sum = 0;

 

  for(int i=0;i<cardNoArr.length;i++) {

 

    sum += cardNoArr[i];

 

  }

 

  return sum % 10 == 0;

 

}
Copy after login

Attached (bank card number formatting):

When entering the bank card number, usually, we will insert a "-" every 4 digits, a format similar to "1332-2131-2313-1231-212"
can be achieved through the following methods:

Set the proxy of textField

- (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;

}
Copy after login

Finally, how to get the bank card number that does not contain the symbol "-"?

[self.textField.text stringByReplacingOccurrencesOfString:@"-" withString:@""]

The above is the detailed content of Method to determine whether the bank card number entered by the user is correct_regular expression. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Disabling Win11 Input Experience Guide Disabling Win11 Input Experience Guide Dec 27, 2023 am 11:07 AM

Recently, many Win11 users have encountered the problem that the input experience dialog box always flickers and cannot be turned off. This is actually caused by the default system services and components of Win11. We need to disable the relevant services first, and then disable the input experience service. Solved, let’s try it out together. How to turn off the input experience in win11: First step, right-click the start menu and open "Task Manager". Second step, find the three processes "CTF Loader", "MicrosoftIME" and "Service Host: Textinput Management Service" in order, right-click "End Task" "The third step, open the start menu, search and open "Services" at the top. The fourth step, find "Textinp" in it

Windows input encounters hang or high memory usage [Fix] Windows input encounters hang or high memory usage [Fix] Feb 19, 2024 pm 10:48 PM

The Windows input experience is a key system service responsible for processing user input from various human interface devices. It starts automatically at system startup and runs in the background. However, sometimes this service may automatically hang or occupy too much memory, resulting in reduced system performance. Therefore, it is crucial to monitor and manage this process in a timely manner to ensure system efficiency and stability. In this article, we will share how to fix issues where the Windows input experience hangs or causes high memory usage. The Windows Input Experience Service does not have a user interface, but it is closely related to handling basic system tasks and functions related to input devices. Its role is to help the Windows system understand every input entered by the user.

Solve win11 search bar input problem Solve win11 search bar input problem Dec 26, 2023 pm 12:07 PM

The search bar is a very useful function in the win11 system, which can help us find the settings, functions and services we want. However, some friends have encountered the situation where the win11 search bar cannot be entered. We can modify the relevant data in the registry to solve the problem. Let's follow the editor to solve it. What to do if you can’t type in the win11 search bar 1. First, we can press “win+r” on the keyboard to bring up run. 2. Then enter "regedit" and press Enter to open the Registry Editor. 3. Then enter "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Expl" in the path above

How to input word matrix How to input word matrix Mar 19, 2024 pm 11:00 PM

Matrix is ​​a formula we often use and encounter, but do you know how to input a matrix in word? Many people may have never been exposed to it and will feel confused when using it. So, here we will share with you how to input the word matrix. I hope that sharing this technique can bring help and inspiration to you. 1. First, create and open a word document. In order to demonstrate the operation, we will use a blank document for demonstration. As shown in the figure below: 2. If we want to input the matrix, we need to find the [Insert] button in the menu bar. This button can insert many content options, such as pictures, etc., all from this menu bar. 3. After clicking [Insert], look on the right side of our tool options and you can find [Formula], and then we click [

C program to input an array of sequences of integers separated by spaces C program to input an array of sequences of integers separated by spaces Aug 25, 2023 am 11:33 AM

Problem Statement Write a C program that takes space separated integers as array input. SampleExamples input 12345 output ‘Arrayelementsare-’1,2,3,4,5Explanation’s Chinese translation is: The explanation input contains 5 integers separated by spaces. Input 997687542356878967343423 Output ‘Arrayelementsare-’99,76,87,54,23,56,878,967,34,34,23 The Chinese translation of Explanation is: Explanation The input contains 11 integers separated by spaces. Method 1 In this method, we will replace the input with empty

Notes and Examples: How to Correctly Enter Floating Point Numbers in Python Notes and Examples: How to Correctly Enter Floating Point Numbers in Python Feb 02, 2024 pm 06:34 PM

Notes and examples of Python floating-point input In Python, floating-point numbers are a common data type used to represent values ​​with decimal parts. When making floating-point input, there are some things we need to know and pay attention to to ensure the correctness and accuracy of the input. This article will introduce several common considerations and provide sample code to deepen understanding. Floating-point input methods In Python, there are many floating-point input methods. The common ones are as follows: Use floating-point numbers directly for input: for example: x

PHP Practice Guide: How to determine if the input contains only numbers and letters PHP Practice Guide: How to determine if the input contains only numbers and letters Mar 28, 2024 pm 03:06 PM

In PHP programming, sometimes we need to limit the data input by the user, such as determining whether the input only contains numbers and letters. This is often encountered in actual project development, so it is very important to master how to implement this function. This article will introduce how to use PHP to determine that the input only contains numbers and letters, and give specific code examples. Why do you need to determine that the input only contains numbers and letters? In website development, the data entered by the user may be used for important functions such as database operations and file operations. If the data entered by the user contains special

The Bank of Korea plans to launch a digital currency pilot next year with 100,000 citizens participating The Bank of Korea plans to launch a digital currency pilot next year with 100,000 citizens participating Dec 02, 2023 am 09:25 AM

According to news from this site on December 2, the day after Bank for International Settlements General Manager Agustín Carstens visited Seoul, the Bank of Korea (BOK) announced details of the digital currency (CBDC) pilot plan, stating that there will be 10 Ten thousand selected South Korean citizens will join the pilot in the fourth quarter of next year. This website noticed that participants can use CBDC issued by commercial banks to purchase goods. The Bank of Korea, the Financial Services Commission and the Financial Supervisory Service plan to propose joint pilot tasks for banks participating in the test and separate separate tasks for each bank after consulting with relevant organizations and reviewing relevant laws. The central bank said digital currencies could solve problems with existing voucher systems, such as those used during COVID-19 or the government childcare grant system. Source

See all articles