Skip to content

Instantly share code, notes, and snippets.

@KensoDev
Created January 18, 2010 05:59
Show Gist options
  • Save KensoDev/279817 to your computer and use it in GitHub Desktop.
Save KensoDev/279817 to your computer and use it in GitHub Desktop.
/// <summary>
/// Validates the VISA credit card.
/// </summary>
/// <param name="creditCardNumber">The credit card number.</param>
/// <returns></returns>
public static bool validateVISACreditCard(string creditCardNumber)
{
// this is a short credit card, the algorithm is unknown so return true
//ISRACARD
if (creditCardNumber.Length == 8 || creditCardNumber.Length == 9)
return true;
// this is a normal credit card (VISA or ISRACARD) so continue checking, if the lenght is invalid return false;
if (creditCardNumber.Length > 19 || creditCardNumber.Length < 11)
return false;
//VISA
// seperate all the numbers into an array of chars
char[] numbers = creditCardNumber.ToCharArray();
//declare the weight number var
int weightNumber = 0;
//the kength of the array
int length = numbers.Length;
// the current index of the array
int currIndex = length - 1;
List<CreditCardNumbers> creditCardNumbersList = new List<CreditCardNumbers>();
// while the loop is shorter then the length of the char array
// we are using this way beacue we need the credit card number from right to left and not the other way around
while (currIndex >= 0)
{
//if the current index is an even number then the weightNumber is 2
// else is 1
weightNumber = NumberHelpers.isOddNumber(currIndex) ? 2 : 1;
//add the number and the weight number to a list
creditCardNumbersList.Add(new CreditCardNumbers(Convert.ToInt32(numbers[currIndex].ToString()), weightNumber));
//subtract 1 off the index
currIndex--;
}
// reset the counter of the index
currIndex = length - 1;
// var for the summing of all the weight numbers after the precious calculation
int weightNumbersSum = 0;
foreach (CreditCardNumbers item in creditCardNumbersList)
{
// weight number var
weightNumber = item.weightNumber;
// credit number var
int creditNumber = item.number;
// this is the calculated number of the weight number times the credit number
int calculatedNumber = weightNumber * creditNumber;
// seperate the calculated number into an array
char[] calculatedWeightNumbers = calculatedNumber.ToString().ToCharArray();
// if the length of the result is more then 2 numbers, add the first to the second
// if the result 12
// 1 + 2 ==> 3 --> this is the final expected result
if (calculatedWeightNumbers.Length > 1)
calculatedNumber = (Convert.ToInt32(calculatedWeightNumbers[0].ToString()) + Convert.ToInt32(calculatedWeightNumbers[1].ToString()));
//sum up the calculated numbers
weightNumbersSum += calculatedNumber;
}
// if the final result is divided by 10, the credit card number is valid
if (weightNumbersSum % 10 == 0)
return true;
else
return false;
}
@etodanik
Copy link

I wrote up a little article about ISRACARD validation:
http://povolotski.me/2013/09/24/isracard-credit-card-number-validation-2/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment