标签:
// 判断银行卡卡号输入的合法性 //参数:输入银行卡号,判断银行卡号是否为合法的银行卡卡号(YES 为合法) - (BOOL) checkCardNo:(NSString*) cardNo;
#pragma mark ---——————————————————————————判断银行卡卡号输入的合法性---—————————————————————————— - (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://www.cnblogs.com/benpaobadaniu/p/5330249.html