码迷,mamicode.com
首页 > 其他好文 > 详细

判断银行账号是否输入正确

时间:2015-04-08 13:17:50      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

不多说  直接贴代码   随便输入银行卡号就可以进行判断
- (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;
}


判断银行账号是否输入正确

标签:

原文地址:http://blog.csdn.net/lixianyue1991/article/details/44938309

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!