IOS判断是否有效银行卡号

IOS判断是否有效银行卡号

by  伍雪颖

+ (BOOL) isValidCreditNumber:(NSString*)value {
    BOOL result = NO;
    NSInteger length = [value length];
    if (length >= 13) {
        result = [WTCreditCard isValidNumber:value];
        if (result)
        {
            NSInteger twoDigitBeginValue = [[value substringWithRange:NSMakeRange(0, 2)] integerValue];
            //VISA
            if([WTCreditCard isStartWith:value Str:@"4"]) {
                if (13 == length||16 == length) {
                    result = TRUE;
                }else {
                    result = NO;
                }
            }
            //MasterCard
            else if(twoDigitBeginValue >= 51 && twoDigitBeginValue <= 55 && length == 16) {
                result = TRUE;
            }
            //American Express
            else if(([WTCreditCard isStartWith:value Str:@"34"]||[WTCreditCard isStartWith:value Str:@"37"]) && length == 15){
                result = TRUE;
            }
            //Discover
            else if([WTCreditCard isStartWith:value Str:@"6011"] && length == 16) {
                result = TRUE;
            }else {
                result = FALSE;
            }
        }
        if (result)
        {
            NSInteger digitValue;
            NSInteger checkSum = 0;
            NSInteger index = 0;
            NSInteger leftIndex;
            //even length, odd index
            if (0 == length%2) {
                index = 0;
                leftIndex = 1;
            }
            //odd length, even index
            else {
                index = 1;
                leftIndex = 0;
            }
            while (index < length) {
                digitValue = [[value substringWithRange:NSMakeRange(index, 1)] integerValue];
                digitValue = digitValue*2;
                if (digitValue >= 10)
                {
                    checkSum += digitValue/10 + digitValue%10;
                }
                else
                {
                    checkSum += digitValue;
                }
                digitValue = [[value substringWithRange:NSMakeRange(leftIndex, 1)] integerValue];
                checkSum += digitValue;
                index += 2;
                leftIndex += 2;
            }
            result = (0 == checkSum%10) ? TRUE:FALSE;
        }
    }else {
        result = NO;
    }
    return result;
}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。