SRL Archive - General

Program that checks valid credit card number!


rj

2013-09-14 20:56:45

Don't get to excited about the title, this program just checks if a entered number could be valid not if it is someones actual credit card number. So my math teacher thinks hes l33t by teaching us if credit card number are valid, well I decided to make a little simba program to check(Credits to Jani for Int to array function(edited a little by me) and kasi for the odd number help(got mixed up -.-))


An American credit card number is valid if:

It has 16 digits.
All of the odd number added and multiplied by 2 plus the odd digits over 4 added together, plus the even digits added together ends in 4

var
number:string;

function IntDigits(int: Int64): TIntegerArray;
var
s: string;
l, i: Integer;
begin
s := Int64ToStr(int);
l := Length(s);
SetLength(Result, l);
for i := 0 to (l - 1) do
Result[i] := StrToInt(s[(i + 1)]);
end;

procedure TIAAppend(var TIA: TIntegerArray; x: Integer);
var
aL: Integer;
begin
aL := (Length(TIA) + 1);
SetLength(TIA, aL);
TIA[(aL - 1)] := Integer(x);
end;

function even_number(Number: integer): boolean;
begin
result := (Number mod 2 = 0);
end;

function add_TIA(arr:TIntegerArray):Integer;
var
i:Integer;
begin
for i := 0 to high(arr) do
result := result + arr[i];
end;

function check_valid_credit(number:Int64):Boolean;
var
card_number, even, odd:TIntegerArray;
i, total:Integer;
begin
card_number := IntDigits(number);
if (length(card_number) <> 16) then
begin
result := false;
exit;
end;
for i := 0 to high(card_number) do
begin
if even_number(i) then
TIAAppend(even, card_number[i]);
if (not even_number(i)) then
TIAAppend(odd, card_number[i]);
end;
total := total + (add_TIA(odd) * 2) + add_TIA(even);
for i := 0 to high(odd) do
begin
if (odd[i] > 4) then
total := total + 1
end;
result := (total mod 10 = 0);
end;

var
t:Integer;
begin
InputQuery('Credit Card number', 'Enter a credit card number', number);
case check_valid_credit(StrToInt64(number)) of
True: Writeln('You entered a valid credit card number!');
false: Writeln('You entered a invalid credit card number!');
end;
end.

meh on the side note I was going to make it generate random credit card numbers… but meh can't generate a random int64 (or can you?)

Turpinator

2013-09-14 21:07:49

 function RandomCC: Int64;
var
s: string;
begin
s := '';
while length(s) <= 16 do
s := s + IntToStr(random(11));
result := StrToInt64(s);
end;

that one seems better.

Nebula

2013-09-14 21:56:11

said my number was invalid

rj

2013-09-14 21:57:25

From: Nebula
said my number was invalid


If it's not 16 digits then it won't work only works for 16 digits cards

I know it works for Mastercard and wells fargo

Nebula

2013-09-14 22:10:46

From: Officer Barbrady
If it's not 16 digits then it won't work only works for 16 digits cards

I know it works for Mastercard and wells fargo


i have visa, its 16 digits, didnt work.

Brandon

2013-09-14 22:14:28

From: Turpinator
That one seems better.


Hmm.. That seems too slow..



Function IsEven(I: Integer): Boolean;
Begin
Result := (I and 1) = 0; //Small Optimization..
End;

Function Random64(): Int64;
Begin
Result := Random(High(Int64));
End;

Function Random64(I: Int64): Int64; overload;
Begin
Result := Random(I);
End;

Function RandomRange64(Min, Max: Int64): Int64;
Begin
Result := Min + (Random64() mod (Max - Min + 1))
End;

Function ValidCreditCard(I: Int64): Boolean;
Begin
Result := (I >= 1000000000000000) And (I <= 9999999999999999);
End;

begin
//Generate random 16 digit 64-bit Integers.
writeln(RandomRange64(1000000000000000, 9999999999999999));
writeln(ValidCreditCard(RandomRange64(1000000000000000, 9999999999999999)));
end.


@OP.. you have redundant code:

{$IFDEF Lape}
s := Int64ToStr(int);
{$ELSE}
s := Int64ToStr(int);
{$ENDIF}

rj

2013-09-14 22:18:33

From: Brandon
..


Woops the redundant code part is from when jani's version had Abs for lape and iAbs for pascal script, I removed that so it would work with int64 and didn't remove the ifdef parts thanks :d

dogrin

2013-09-15 13:57:16

Thats amazing, I would of never thought the numbers had a meaning behind it.

JennyCocoX

2013-09-15 14:03:10

this is truly amazing work thank you. I wish I could do something like this one day :')

rj

2013-09-17 20:29:36

package project;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;

public class Validate {

public static int[] intDigits(long Int)
{
String s = Long.toString(Int);
int[] result = new int[s.length()];

for (int i = 0; i < s.length(); i++)
{
result[i] = Integer.parseInt(s.substring(i, i + 1));
}
return result;
}

public static boolean evenNumber(int number)
{
return (number % 2 == 0);
}

public static int add_TIA(ArrayList<Integer> arr)
{
int result = 0;
for (int i = 0; i < arr.size(); i++)
{
result += arr.get(i);
}
return result;
}
public static boolean checkValid(String number)
{
ArrayList<Integer> odd = new ArrayList();
ArrayList<Integer> even = new ArrayList();

int [] cardNumber;
int total = 0;
if (number == "")
{
return false;
}
cardNumber = intDigits(Long.parseLong(number));
if (cardNumber.length != 16)
{
System.out.println("Invalid number of charactors");
return false;
}
for (int i = 0; i < cardNumber.length; i++)
{
if (evenNumber(i))
{
even.add(cardNumber[i]);
}
if (!evenNumber(i))
{
odd.add(cardNumber[i]);
}
}
total += (add_TIA(odd) * 2) + (add_TIA(even));
for (int i = 0; i < odd.size(); i++)
{
if (odd.get(i) > 4)
{
total += 1;
}
}

return (total % 10 == 0);
}

public static int random(int amt)
{
Random randomGenerator = new Random();
return randomGenerator.nextInt(amt);
}
public static String generateID()
{
String s = "";
while (s.length() <= 15)
{
s += Integer.toString(random(10));
}
return s;
}

public static void main(String[] args)
{

Scanner keyboard = new Scanner(System.in);
String Card;
System.out.println("Enter in a credit card number");
Card = keyboard.nextLine();
System.out.println(checkValid(Card));
}

}

b0bzilla

2013-09-18 08:58:45

Your teacher gave you the wrong algorithm.

The last digit of a credit card is a check digit that can be obtained from the rest of the card.

Web search "credit card check digit validate mod 10" and you will find some pages that describe the correct calculation.

rj

2013-09-18 15:02:39

From: b0bzilla
Your teacher gave you the wrong algorithm.

The last digit of a credit card is a check digit that can be obtained from the rest of the card.

Web search "credit card check digit validate mod 10" and you will find some pages that describe the correct calculation.

Not every credit card company uses the same method, and considering my program worked with my credit card and a couple examples then I can assume it works for Visa not sure about others

b0bzilla

2013-09-19 08:06:31

From: Officer Barbrady
Not every credit card company uses the same method, and considering my program worked with my credit card and a couple examples then I can assume it works for Visa not sure about others


They do all use the same method (at least MC/Visa/Amex/Diners/Discover/JCB). It's known as the Luhn algorithm.

I worked for a credit card processor and this is how they check card numbers. Prefix, length, check digit.

I don't know if the method you posted is valid for some cards based on math equivalence. Didn't look at it that closely.

I just noticed some people posting that it didn't work for their cards and I knew it wasn't the method card companies use to perform validation.

rj

2013-09-19 11:16:07

From: b0bzilla
They do all use the same method (at least MC/Visa/Amex/Diners/Discover/JCB). It's known as the Luhn algorithm.

I worked for a credit card processor and this is how they check card numbers. Prefix, length, check digit.

I don't know if the method you posted is valid for some cards based on math equivalence. Didn't look at it that closely.

I just noticed some people posting that it didn't work for their cards and I knew it wasn't the method card companies use to perform validation.


Worked for mine, worked for example on my homework.