銀行卡Regex

來源:互聯網
上載者:User

標籤:

  1. - (void)viewDidLoad {  
  2.     [super viewDidLoad];  
  3.     NSString *str = @"6226820011200783033";  
  4.     BOOL isRight = [self checkCardNo:str];  
  5.     if (!isRight) {  
  6.           
  7.         UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"不對" message:@"請重新輸入卡號" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil nil];  
  8.         [alert show];  
  9.     }else{  
  10.         UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"對" message:@"" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil nil];  
  11.         [alert show];}  
  12. }  
  13.  

  14.  

  15.  

  16.  

  17.  

  18. - (BOOL) checkCardNo:(NSString*) cardNo{  

  19.     int oddsum = 0;     //奇數求和  
  20.     int evensum = 0;    //偶數求和  
  21.     int allsum = 0;  
  22.     int cardNoLength = (int)[cardNo length];  
  23.     int lastNum = [[cardNo substringFromIndex:cardNoLength-1] intValue];  
  24.       
  25.     cardNo = [cardNo substringToIndex:cardNoLength - 1];  
  26.     for (int i = cardNoLength -1 ; i>=1;i--) {  
  27.         NSString *tmpString = [cardNo substringWithRange:NSMakeRange(i-1, 1)];  
  28.         int tmpVal = [tmpString intValue];  
  29.         if (cardNoLength % 2 ==1 ) {  
  30.             if((i % 2) == 0){  
  31.                 tmpVal *= 2;  
  32.                 if(tmpVal>=10)  
  33.                     tmpVal -= 9;  
  34.                 evensum += tmpVal;  
  35.             }else{  
  36.                 oddsum += tmpVal;  
  37.             }  
  38.         }else{  
  39.             if((i % 2) == 1){  
  40.                 tmpVal *= 2;  
  41.                 if(tmpVal>=10)  
  42.                     tmpVal -= 9;  
  43.                 evensum += tmpVal;  
  44.             }else{  
  45.                 oddsum += tmpVal;  
  46.             }  
  47.         }  
  48.     }  
  49.       
  50.     allsum = oddsum + evensum;  
  51.     allsum += lastNum;  
  52.     if((allsum % 10) == 0)  
  53.         return YES;  
  54.     else  
  55.         return NO;  
  56. }  

銀行卡Regex

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.