Results 1 to 14 of 14

Thread: Program that checks valid credit card number!

  1. #1
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default Program that checks valid credit card number!

    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

    Simba Code:
    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?)

  2. #2
    Join Date
    Jan 2012
    Posts
    1,596
    Mentioned
    78 Post(s)
    Quoted
    826 Post(s)

    Default

    Simba Code:
    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.

  3. #3
    Join Date
    Dec 2011
    Location
    New York, USA
    Posts
    1,242
    Mentioned
    12 Post(s)
    Quoted
    193 Post(s)

    Default

    said my number was invalid

  4. #4
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by Nebula View Post
    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

  5. #5
    Join Date
    Dec 2011
    Location
    New York, USA
    Posts
    1,242
    Mentioned
    12 Post(s)
    Quoted
    193 Post(s)

    Default

    Quote Originally Posted by Officer Barbrady View Post
    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.

  6. #6
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by Turpinator View Post
    That one seems better.
    Hmm.. That seems too slow..


    Simba Code:
    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:
    Simba Code:
    {$IFDEF Lape}
      s := Int64ToStr(int);
    {$ELSE}
      s := Int64ToStr(int);
    {$ENDIF}
    Last edited by Brandon; 09-15-2013 at 02:23 AM.
    I am Ggzz..
    Hackintosher

  7. #7
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by Brandon View Post
    ..
    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

  8. #8
    Join Date
    Mar 2012
    Posts
    11
    Mentioned
    0 Post(s)
    Quoted
    5 Post(s)

    Default

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

  9. #9
    Join Date
    Sep 2013
    Posts
    49
    Mentioned
    1 Post(s)
    Quoted
    40 Post(s)

    Default

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

  10. #10
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    java Code:
    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));
        }  

    }

  11. #11
    Join Date
    Aug 2013
    Location
    East Coast USA
    Posts
    14
    Mentioned
    0 Post(s)
    Quoted
    5 Post(s)

    Default

    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.

  12. #12
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by b0bzilla View Post
    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

  13. #13
    Join Date
    Aug 2013
    Location
    East Coast USA
    Posts
    14
    Mentioned
    0 Post(s)
    Quoted
    5 Post(s)

    Default

    Quote Originally Posted by Officer Barbrady View Post
    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.

  14. #14
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by b0bzilla View Post
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •