簡體   English   中英

iPhone Sdk中的信用卡驗證算法

[英]Credit Card Validation Algorithm in iPhone Sdk

任何人都可以分享信用卡驗證算法的任何示例代碼。

Luhn算法:

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  
-(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;
}

}

在您的應用程序中,您只能進行信用卡號驗證而不是驗證(銀行必須這樣做)。 請參閱此正則表達式網站

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM