判断银行卡号是否有效
//剔除卡号里的非法字符
-(NSString *)getDigitsOnly:(NSString*)s
{
NSString *digitsOnly = @"";
char c;
for (int i = 0; i < s.length; i++)
{
c = [s characterAtIndex:i];
if (isdigit(c))
{
digitsOnly =[digitsOnly stringByAppendingFormat:@"%c",c];
}
}
return digitsOnly;
}
//检查银行卡是否合法
//Luhn算法
-(BOOL)isValidCardNumber:(NSString *)cardNumber
{
NSString *digitsOnly = [self getDigitsOnly:cardNumber];
int sum = 0;
int digit = 0;
int addend = 0;
BOOL timesTwo = false;
for (int i = digitsOnly.length - 1; i >= 0; i--)
{
digit = [digitsOnly characterAtIndex:i] - '0';
if (timesTwo)
{
addend = digit * 2;
if (addend > 9) {
addend -= 9;
}
}
else {
addend = digit;
}
sum += addend;
timesTwo = !timesTwo;
}
int modulus = sum % 10;
return modulus == 0;
}