How To Verify Credit-Card Numbers 
Author Message
 How To Verify Credit-Card Numbers

(*

  Here's an example of how to verify and identify a credit-card
  number (the card number -- not its credit-worthiness).

  This code works for Visa, MasterCard, and American Express...

*)
(************************* CARDCHEK.MOD ***************************)
MODULE CardChek;
(*
**  Filename: CARDCHEK.MOD  (Modula-2)
**
**  Purpose:  Verify Visa / MasterCard / AmEx credit-card number.
**
**  System:   FST Modula-2
**
**  Author:   Gregory S. Vigneault
**            Embedded Systems Developer
**             http://www.*-*-*.com/ ~telic
**
**  Edit:     March 17, 1999
**
*)
FROM InOut IMPORT ReadString, Write, WriteLn, WriteString;
IMPORT ASCII, Strings;

TYPE Chars = SET OF CHAR;

(*----------------------------------------------------------------*)

PROCEDURE Val (ch :CHAR) :CARDINAL;
  (* Return raw 0..9 for '0'..'9' *)
  BEGIN
    RETURN ORD(ch) - ORD('0');
  END Val;

(*----------------------------------------------------------------*)

PROCEDURE VeriCard (cardnum : ARRAY OF CHAR) : CARDINAL;
  (* Verify Visa / MasterCard / AmericanExpress card number *)
  VAR
    cardn : ARRAY [1..32] OF CHAR;
    w, x, y, z : CARDINAL;

  BEGIN

    (* strip out non-numeric characters from card number string *)
    y := 1;
    x := 0;
    z := 0;
    WHILE (x <= HIGH(cardnum)) AND (cardnum[x] <> ASCII.nul) DO
      IF (cardnum[x] IN Chars{'0'..'9'}) AND (y <= HIGH(cardn)) THEN
        cardn[y] := cardnum[x];
        INC(y);
        IF y <= HIGH(cardn) THEN (* insert string terminator *)
          cardn[y] := ASCII.nul;
        END;
        INC(z); (*length of digits string*)
      END;
      INC(x);
    END;

    (* calculate checksum... *)
    w := 0;
    FOR x := (z - 1) TO 1 BY -1 DO
      y := Val(cardn[x]);
      IF NOT ODD(z) = ODD(x) THEN
        y := y + y;
        IF y >= 10 THEN
          y := (y - 10) + 1;
        END;
      END;
      w := w + y;
    END;

    x := 10 - (w MOD 10);
    IF x = 10 THEN
      x := 0;
    END;

    (*
    **  Return one of four possible results...
    **
    **  0 = Invalid/Unknown card type
    **  3 = American Express
    **  4 = Visa
    **  5 = MasterCard
    *)

    IF x = Val(cardn[z]) THEN
      RETURN Val(cardn[1]);
    ELSE
      RETURN 0;
    END;

  END VeriCard;

(*----------------------------------------------------------------*)

VAR

  ccard   : ARRAY [0..31] OF CHAR;
  result  : CARDINAL;

BEGIN

  WriteLn;
  WriteString("Visa / MasterCard / American_Express Number Check.");
  WriteLn; WriteLn;
  WriteString("Enter number with hyphens, e.g. 0000-000-000-000");
  WriteLn;
  WriteString("(You can abort this program by entering 0)");
  WriteLn; WriteLn;
  WriteString("Card number to check?: ");
  ReadString(ccard);
  WriteLn; WriteLn;

  IF Strings.Length(ccard) > 12 THEN
    result := VeriCard(ccard);
    IF (result >= 3) AND (result <= 5) THEN
      WriteString("+++ Valid ");
      CASE result OF
        3 : WriteString("AMERICAN EXPRESS");
      | 4 : WriteString("VISA");
      | 5 : WriteString("MASTERCARD");
      ELSE
        (*should never get here*)
      END; (*CASE*)
    ELSE
      WriteString("!!! Unknown or invalid");
    END;
    WriteString(" card number.");
  ELSE
    WriteString("??? Not enough digits!");
  END;

  WriteLn;

END CardChek.
(************************* CARDCHEK.MOD ***************************)
(*

  Greg_

  http://www.*-*-*.com/ ~telic
  Wednesday, March 17, 1999, Toronto, Canada.

*)



Sun, 02 Sep 2001 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. verifying credit card numbers

2. Credit Card Number Mask

3. HOW TO VALIDATE CREDIT CARD NUMBERS?

4. Validate Credit Card Numbers

5. Secure, Reliable way of handling Credit Card numbers

6. Credit card numbers, store in dbase or encrypt.

7. using m2crypto to encrypt credit card numbers

8. APL94 and credit cards

9. ACCEPT MAJOR CREDIT CARDS !!!!!!

10. ACCEPT MAJOR CREDIT CARDS !!!!!!

11. Credit card charging code

 

 
Powered by phpBB® Forum Software