Validate the credit card (oracle 10 g plsql code) – working!

Posted by Jay     29 January, 2009    2,611 views   

The Luhn algorithm or Luhn formula, also known as the “modulus 10″ or “mod 10″ algorithm, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers and Canadian Social Insurance Numbers.

Recently, Oracle has stopped validating the credit card numbers as there is match for Authorization of credit card. However, this rudimentary mod-10 LUHN algorithm is still valid and valuable for us to check.

Hence, I started creating this mod-10 LUHN algorithm in plsql. This is tested with oracle 10 g database and working well. Special Thanks to ashish who has fixed the bug and tested the code.

Download the credit_card_validator.zip

–===========================================================================
– Copyright 2008 Author:Jiltin
–=============================================================================
– DESCRIPTION
–    I have been searching for a plsql package to validate the credit card.
–    Eventhough I created a Visual C# program to validate credit card,
–    I was lazy enough to create a plsql. Finally, here you go with LUHN!
–=============================================================================

– table indicates card type, prefix, length and also for known cards
—————-
– CARD TYPE………………..Prefix………Length..Check digit algorithm
– MASTERCARD……………….51-55……….16……mod 10    
– VISA…………………….4…………..13,16…mod 10  
– AMEX…………………….34,37……….15……mod 10  
– Diners Club………………300-305,36,38..14……mod 10  
– Discover…………………6011………..16……mod 10    
– enRoute………………….2014,2149……15……any      
– JCB……………………..3…………..16……mod 10    
– JCB……………………..2131,1800……15……mod 10    
–===========================================================================

–VALIDATECREDITCARD(’371536720471006′)

CREATE OR REPLACE FUNCTION validatecreditcard (p_creditcardnumber IN VARCHAR2)
   RETURN VARCHAR2
IS
   creditcardnumber    VARCHAR2 (32);
           – := nosymbols (p_CreditCardNumber, LENGTH (p_CreditCardNumber));
   creditcardlength    NUMBER         := LENGTH (p_creditcardnumber);
   subtotal            NUMBER         := 0;
   t_value             NUMBER         := 0;
   c1                  NUMBER;
   c2                  NUMBER;
   c3                  NUMBER;
   c4                  NUMBER;
   cardtype            VARCHAR2 (160) := ‘CARD’;
   calculationmethod   VARCHAR2 (160) := ‘UNKNOWN’;
   RESULT              VARCHAR2 (160);
BEGIN
   creditcardnumber := LTRIM(RTRIM(p_creditcardnumber));
   creditcardnumber := REPLACE(creditcardnumber, ‘-’, );
   creditcardnumber := REPLACE(creditcardnumber, ‘.’, );
   
–IF isnumber (CreditCardNumber) = 0 THEN

   c1 := TO_NUMBER (SUBSTR (creditcardnumber, 1, 1));
   c2 := TO_NUMBER (SUBSTR (creditcardnumber, 1, 2));
   c3 := TO_NUMBER (SUBSTR (creditcardnumber, 1, 3));
   c4 := TO_NUMBER (SUBSTR (creditcardnumber, 1, 4));

   IF creditcardlength = 13
   THEN
      IF c1 IN (4)
      THEN
         cardtype := ‘VISA’;
         calculationmethod := ‘MOD10′;
      END IF;
   ELSIF creditcardlength = 14
   THEN
      IF c2 IN (36, 38)
      THEN
         cardtype := ‘DINERS CLUB’;
         calculationmethod := ‘MOD10′;
      ELSIF c3 IN (300, 301, 302, 303, 304, 305)
      THEN
         cardtype := ‘DINERS CLUB’;
         calculationmethod := ‘MOD10′;
      END IF;
   ELSIF creditcardlength = 15
   THEN
      IF c2 IN (34, 37)
      THEN
         cardtype := ‘AMEX’;
         calculationmethod := ‘MOD10′;
      ELSIF c4 IN (2014, 2149)
      THEN
         cardtype := ‘enROUTE’;
         calculationmethod := ‘ANY’;
      ELSIF c4 IN (2131, 1800)
      THEN
         cardtype := ‘JBC’;
         calculationmethod := ‘MOD10′;
      END IF;
   ELSIF creditcardlength = 16
   THEN
      IF c1 IN (4)
      THEN
         cardtype := ‘VISA’;
         calculationmethod := ‘MOD10′;
      ELSIF c1 IN (3)
      THEN
         cardtype := ‘JBC’;
         calculationmethod := ‘MOD10′;
      ELSIF c2 IN (51, 52, 53, 54, 55)
      THEN
         cardtype := ‘MASTERCARD’;
         calculationmethod := ‘MOD10′;
      ELSIF c4 IN (6011)
      THEN
         cardtype := ‘DISCOVER’;
         calculationmethod := ‘MOD10′;
      END IF;
   END IF;

   IF calculationmethod = ‘MOD10′
   THEN
      FOR i IN REVERSE 1 .. LENGTH (creditcardnumber)
      LOOP
             IF cardtype = ‘AMEX’
         THEN
            IF (TO_NUMBER (SUBSTR (TO_CHAR (i), LENGTH (i), 1)) NOT IN  (1, 3, 5, 7, 9))
            THEN
               t_value := SUBSTR (creditcardnumber, i, 1) * 2;
               subtotal := subtotal + SUBSTR (t_value, 1, 1);
               subtotal := subtotal + NVL (SUBSTR (t_value, 2, 1), 0);
            ELSE
               subtotal := subtotal + SUBSTR (creditcardnumber, i, 1);
            END IF;                        
         ELSE          
            IF (TO_NUMBER (SUBSTR (TO_CHAR (i), LENGTH (i), 1)) IN  (1, 3, 5, 7, 9))
            THEN
               t_value := SUBSTR (creditcardnumber, i, 1) * 2;
               subtotal := subtotal + SUBSTR (t_value, 1, 1);
               subtotal := subtotal + NVL (SUBSTR (t_value, 2, 1), 0);
            ELSE
               subtotal := subtotal + SUBSTR (creditcardnumber, i, 1);
            END IF;                       
         END IF;
      END LOOP;

      IF MOD (subtotal, 10) = 0
      THEN
         RESULT := ‘VALID’;
      ELSE
         RESULT := ‘INVALID’;
      END IF;
   ELSIF calculationmethod = ‘ANY’
   THEN
      RESULT := ‘VALID’;
   ELSE
      RESULT := ‘UNKNOWN’;
   END IF;

   RESULT := RESULT ‘ ‘ cardtype;
   RETURN (RESULT);
END;
/

Following Google Searches Lead To This Post: +oracle +modulo +sample
oracle username validation in pl/sql
oracle & check valid date
credit card checksum algorithm plsql
validate luhn oracle
mod10 plsql
“módulo 11″ pl/sql
pl sql + validate email address
c program validate credit cards
nsis substring
pl sql validate number
modulo 10 check sql
luhn pl/sql
oracle pl sql number validation
luhn algorithm + pl/sql;
validate passwords using pl/sql
checksum calculation algorithm oracle
how to validate column length in plsql
validating text in nsis
plsql substr length
oracle pl sql Check Digit Mod10
validar string com numero pl sql oracle
code for finding the the last digit in a number in oracle
plsql luhn
The length or prefix of the card number is invalid for card brand Discover
jbc credit card validation
oracle pl/sql modulus 10
oracle pl/sql “mod 10″ algorithm
PL/SQL luhn algorithm
PLSQL oracle IP then else esempio
pl sql substr ( to_char
ORACLE PL SQL FUNCTION TO CALCULATE CHECK DIGIT
luhn validation plsql
convert date to “financial year” + “regular expression”
phone number validation code in pl/sql
date regular expression for financial year
plgpsql check digit
validate number + pl/sql
Luhn Function PL/SQL
how to make credit card code ten
mod10 oracle
regular expression credit card number “oracle”
regular expression + calculate financial year from date
luhn algorithm plsql
modulus 10 check digit calculation oracle function
pl/sql function to validate credit card
isnumber regex pl/sql
luhn plsql
validate number pl sql
luhn oracle function
oracle unknown credit card
Credit Card Validation in Oracle
oracle valid date
validate password plsql
posted valid cc numbers with names
validate date pl sql
code and farmula for financial year php mysql
oracle pl/sql procedure passoword validation code
plsql bank details validation
pl/sql functions to validate the data
G code card numbers
does oracle validating SIN numbers
does oracle validate SIN numbers
oracle validating Social Insurance Number
c# program to validate credit card number
reversing of digit in pl/sql using substring
oracle password validation
validate ZIP codes pl/sql
login and password validation in pl sql
oracle mod 10 check digit
create credit card numbers c++
name and valid date creditcards
credit card validation algorithm IN PLSQL
isnumber pl/SQL
oracle PL SQl password validation function
pl/sql credit card validator
+calculate check digit +sql +”mod 10″
credit calculation oracle pl sql
credit card date in oracle
luhn check digit calculator
how to write a pl/sql function which checks credit card no:s
plsql financial calc
luhn mod 10 calculator
check valid phone number from oracle sql
how to validate phone no from oracle sql query
regular expression+substr+pl sql
substr expresion in PL SQL
credit card validation in c#
validate a credit card name
valid number +oracle
pl/sql program for finding a reverse
pl/sql code for calculate balance to payment
sample code in pl sql to calculate the average daily balance
oracle credit card validation
luhn check digit calculator pl/sql
plsql calculate mod10 formula
pl sql code for average daily account balance calculation
plsql luhn algorithm
free code for mod-10 algorithm in c++
Oracle LUHN check
validate and check in pl/sql
validate numbers >10
check digit mod 10+pl/sql procedure
Logic for credit car number validation
plsql mail validate regexp
check digit “mod10″
sql procedure to check credit card
validation using substr in oracle
phone number validation + pl/sql
oracle pl sql check digit
sql function valid telephone number
VALIDATING NUMBERS IN PL/SQL
Oracle SQL Social Security REGEX regular expression
plsql card logic check

Post to Twitter  Post to Delicious  Post to Digg    Post to StumbleUpon

Categories : 11i Scripts, Scripts Oracle Tags : , , , ,

Comments
December 8, 2009

Thanks for the article, it was a good read indeed

Can you provide more information on this?

Posted by china phone
Leave a comment

(required)

(required)