判斷銀行帳號是否輸入正確,判斷銀行帳號輸入

來源:互聯網
上載者:User

判斷銀行帳號是否輸入正確,判斷銀行帳號輸入

不多說  直接貼代碼   隨便輸入銀行卡號就可以進行判斷
- (void)viewDidLoad {    [super viewDidLoad];    NSString *str = @"6226820011200783033";    BOOL isRight = [self checkCardNo:str];    if (!isRight) {                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"不對" message:@"請重新輸入卡號" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];        [alert show];    }else{        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"對" message:@"" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];        [alert show];}}


//這就是判斷方法   

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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.