Home  >  Article  >  Web Front-end  >  Luhn algorithm for matching bank card numbers entered by users

Luhn algorithm for matching bank card numbers entered by users

php中世界最好的语言
php中世界最好的语言Original
2018-03-29 15:55:261955browse

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

Attached (bank card number formatting):

Enter the bank card number During the process, we usually insert a "-" every 4 digits. A format similar to "1332-2131-2313-1231-212"
can be achieved by the following method:

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

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!

Statement:
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