Skip to content

Instantly share code, notes, and snippets.

@HamGuy
Last active December 22, 2015 09:29
Show Gist options
  • Save HamGuy/6452653 to your computer and use it in GitHub Desktop.
Save HamGuy/6452653 to your computer and use it in GitHub Desktop.
Check the credit card is valid or not with ISO 2894 http://en.wikipedia.org/wiki/Luhn_algorithm
#include <string.h>
// Sum Luhn
int GetSumLuhn(
const char * number
)
{
//
int digits = 0;
int i = strlen( number );
char digitv = 0;
//
while( i-- )
{
//
digitv = number - '0';
digitv <<= !( i % 2 );//
digitv -= 9 * ( digitv > 9 );
//
digits += digitv;
}
//
return digits;
}
// CheckSum Luhn
bool CheckSumLuhn(
const char * number
)
{
return ( GetSumLuhn( number ) % 10 == 0 );
}
// Get Luhn Key
char GetKeyLuhn(
const char * number
)
{
return ( 10 - GetSumLuhn( number ) % 10 ) % 10;
}
//
bool CheckKeyLuhn(
const char * number,
const char key
)
{
return ( GetKeyLuhn( number ) == ( key - '0' ) );
}
-(BOOL) validateCardNumber:(NSString *)str
{
BOOL result = NO;
str = [self replaceString:str withString:@"" withPattern:@"[^\\d]"];
if(str!=nil && str.length>0)
{
int num=0;
int weight = (str.length % 2 == 0 )?2:1;
int total=0;
int tmp=0;
for(int i =0;i<[str length];i++)
{
char c = [str characterAtIndex:i] ;
num = (int)c -'0';
tmp = num *weight;
tmp = (tmp>9)?tmp-9:tmp;
total += tmp;
weight = (weight==2)?1:2;
}
result = (total%10)== 0;
}
return result;
}
-(NSString *) replaceString:(NSString *)srcStr withString:(NSString *)replaceStr withPattern:(NSString *)pattern
{
NSMutableString *destStr = [NSMutableString stringWithString:srcStr];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
[regex replaceMatchesInString:destStr options:0 range:NSMakeRange(0, [destStr length]) withTemplate:replaceStr];
return destStr;
}
bool ValidateCard(string str)
{
bool validateResult = false;
//过滤特殊字符,也可在输入的时候过滤
str = Regex.Replace(str,"[^\\d]","");
if (str.Length == 0)
return false;
int num;
int weight = str.Length % 2 == 0 ? 2 : 1;
int total = 0;
int tmp;
foreach (char ch in str)
{
num = (int)ch-'0';
tmp =num * weight;
tmp = (tmp > 9) ? tmp - 9 : tmp;
total += tmp;
weight = weight == 2 ? 1 : 2;
}
validateResult = (total%10) == 0;
return validateResult;
}
//Implemtation with linq
// http://www.codeproject.com/Tips/515367/Validate-credit-card-number-with-Mod-10-algorithm
public static bool Mod10Check(string creditCardNumber)
{
//// check whether input string is null or empty
if (string.IsNullOrEmpty(creditCardNumber))
{
return false;
}
//// 1. Starting with the check digit double the value of every other digit
//// 2. If doubling of a number results in a two digits number, add up
/// the digits to get a single digit number. This will results in eight single digit numbers
//// 3. Get the sum of the digits
int sumOfDigits = creditCardNumber.Where((e) => e >= '0' && e <= '9')
.Reverse()
.Select((e, i) => ((int)e - 48) * (i % 2 == 0 ? 1 : 2))
.Sum((e) => e / 10 + e % 10);
//// If the final sum is divisible by 10, then the credit card number
// is valid. If it is not divisible by 10, the number is invalid.
return sumOfDigits % 10 == 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment