iPhone Sdk 中的信用卡验证算法
Posted
技术标签:
【中文标题】iPhone Sdk 中的信用卡验证算法【英文标题】:Credit Card Validation Algorithm in iPhone Sdk 【发布时间】:2011-10-19 04:13:31 【问题描述】:谁能分享信用卡验证算法的任何示例代码。
【问题讨论】:
【参考方案1】:卢恩算法:
http://en.wikipedia.org/wiki/Luhn_algorithm
这里有一些常用语言的示例:
http://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers
从这个链接,大多数 CC 使用 Luhn(见表格):
http://en.wikipedia.org/wiki/Credit_card_number
从上面的链接(rosettacode):
- (NSMutableArray *) toCharArray
NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[self length]];
for (int i=0; i < [self length]; i++)
NSString *ichar = [NSString stringWithFormat:@"%c", [self characterAtIndex:i]];
[characters addObject:ichar];
return [characters autorelease];
+ (BOOL) luhnCheck:(NSString *)stringToTest
NSMutableArray *stringAsChars = [stringToTest toCharArray];
BOOL isOdd = YES;
int oddSum = 0;
int evenSum = 0;
for (int i = [stringToTest length] - 1; i >= 0; i--)
int digit = [(NSString *)[stringAsChars objectAtIndex:i] intValue];
if (isOdd)
oddSum += digit;
else
evenSum += digit/5 + (2*digit) % 10;
isOdd = !isOdd;
return ((oddSum + evenSum) % 10 == 0);
// results
BOOL test0 = [self luhnCheck:@"49927398716"]; //Result = YES
BOOL test1 = [self luhnCheck:@"49927398717"]; //Result = NO
BOOL test2 = [self luhnCheck:@"1234567812345678"]; //Result = NO
BOOL test3 = [self luhnCheck:@"1234567812345670"]; //Result = YES
【讨论】:
【参考方案2】:在您的申请中,您只能进行信用卡号验证,而不能进行验证(这是银行必须做的事情)。 请参考此regular expression website。
【讨论】:
既然可以做简单的 mod 10 数学运算,为什么还要使用正则表达式? 因为每种卡片卡片类型都有不同的格式。例如,对于 enRoute 类型的信用卡,您的校验位算法将失败。 该表显示 enRoute Active = No 那个正则表达式站点也列出了 6 种类型 - luhn 表显示了 17 种类型。 luhn 算法处理所有不同的格式 - 请参阅表格和信用卡链接。【参考方案3】:-(void)validation_check:(NSString*)pass_value
NSMutableArray *character;
unsigned long long odd_no;
unsigned long long new_odd_no;
unsigned long long even_no;
unsigned long long new_even_no;
unsigned long long multiplied_even_no;
unsigned long long changed_even_no;
unsigned long long final_value;
unsigned long long revers_card_no;
unsigned long long card_no;
unsigned long long check_reverse;
new_odd_no = 0;
new_even_no = 0;
card_no = [pass_value longLongValue];
character = [[NSMutableArray alloc]init];
//-------reversing order of entered card number---------
for(int i = 0; i<[pass_value length];i++)
check_reverse =(card_no % 10);
card_no = (card_no / 10);
[character addObject:[NSString stringWithFormat:@"%qu",check_reverse]];
revers_card_no=revers_card_no*10+check_reverse;
check_reverse=card_no;
pass_value = [NSString stringWithFormat:@"%qu",revers_card_no];
//--------checking for even and odd numbers--------
for(int j=0;j<[character count];j++)
if(j % 2 == 0)
odd_no = [[character objectAtIndex:j]longLongValue];
new_odd_no = new_odd_no+odd_no;
else
//------doubling the value of even no's--------
even_no = [[character objectAtIndex:j]longLongValue];
multiplied_even_no=even_no*2;
NSLog(@"%qu",multiplied_even_no);
//-------if even is a single digit--------
if((multiplied_even_no % 10) == 0)
if(multiplied_even_no == 10)
new_even_no = 1+new_even_no;
else
new_even_no = multiplied_even_no + new_even_no;
//----------if there is multiple digits in even no---------
else
int x=(multiplied_even_no % 10);
int y=multiplied_even_no /10;
changed_even_no = x+y;
new_even_no = new_even_no + changed_even_no;
//--------calculating final value--------
final_value = new_even_no + new_odd_no;
NSLog(@"%qu",final_value);
if((final_value % 10) == 0)
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Card No is valid" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
card_textField.text=nil;
else
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Card No is not valid" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Try again" ,nil];
[alert show];
card_textField.text=nil;
【讨论】:
比必要的复杂得多。避免使用任何常见的 Objective C 习语可获得加分,不包含返回值可加倍加分。以上是关于iPhone Sdk 中的信用卡验证算法的主要内容,如果未能解决你的问题,请参考以下文章