Home  >  Article  >  Web Front-end  >  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

微波
微波Original
2017-06-28 13:39:595243browse

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;

}

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

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;

}

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