Validating Credit Card Numbers

Sahir Shah
September-2002

  Credit card charge approvals usually take a quite a bit of time. You need to pass data on to the bank's server and wait for an approval code before accepting the charge. Validating a credit card number can save an unnecessary delay caused by incorrect input of credit card numbers. This paper explains the checksum algorithm used for validating credit card numbers. 

Checksum Algorithm

    Most credit card numbers are 16 digits, a few (like American Express) have less. The last digit in the card number is the check digit. Take out the last digit and start doing the check sum on the remaining digits from right to left. You alternately multiply the digits by 2 and 1. If the result on multiplication by 2 is greater than 9 then substract 9 from the result. Add the resulting digits together then add the check digit and finally do a modulo 10 check. If the modulo result is positive (ie. result is zero) then the credit card number has been entered correctly. >As an example we shall use a fictitious 16 digit card number : 4934 3789 1236 4804 The last digit 4 is the check digit. If you remove that you are left with 4934 3789 1236 480

4 9 3 4 3 7 8 9 9 2 3 6 4 8 0
4 x 2 9 x 1 3 x 2 4 x 1 3 x 2 7 x 1 8 x 2 9 x 1 9 x 2 2 x 1 3 x 2 6 x 1 4 x 2 8 x 1 0 x 2
8 9 6 4 6 7 16-9=7 9 18-9=9 2 6 6 8 8 0
8 + 9 + 6 + 4 + 6 + 7 + 7 + 9 + 9 + 2 + 6 + 6 + 8 + 8 + 0 = 95 adding the check digit to the result 95 + 4 = 99 since 99 mod 10 is not equal to zero this is not a valid credit card number The following sample program demonstrates how the check can be implemented in code. This is intended to be an example and is therefore not perfect, it just gives you a rough idea of how you can implement this in code.
        public class CreditCard{

        private String ccNbr;
        private Integer digit;
        private boolean isValid;

          public CreditCard(String cardNbr){
           isValid = false;
           ccNbr= stripChars(cardNbr);
           System.out.println(ccNbr);
           verify();
          }

           private String stripChars(String cardNbr){
             // strip spaces
              String test = new String();
              String s = new String();
               for(int j =1; j < cardNbr.length() + 1; j++) {
                   test = cardNbr.substring(j-1 , j);
                     if(!test.equalsIgnoreCase(" ")){
                       s = s + test;
                       }
                  }
               return s;
            }

           public boolean validate(){
             return isValid;
           }

           private void verify(){
            int charVal = 0;int ckDigit = 0;
            boolean flipflop = true; int checkSum = 0;
            int tempVal = 0;
             if(ccNbr.length() < 16) {
                        isValid = false;
                        return;
                    }
              if(ccNbr.length() > 16) {
                   isValid = false;
                   return;
              }
              digit = new Integer(ccNbr.substring(ccNbr.length()-1 , ccNbr.length()));
              ckDigit = digit.intValue();
              for(int i= ccNbr.length()-1; i > 0 ; i--)
              {
                  try{charVal = ccNbr.charAt(i);}
                  catch(Exception e) { isValid = false; break; }
                  if(charVal > 57 || charVal < 48) {
                        isValid = false;
                        return;
                    }
                  try { digit = new Integer(ccNbr.substring(i-1 , i)); }
                  catch(Exception e) { isValid = false; break; }
                  charVal = digit.intValue();
                    if(flipflop){
                      flipflop = false;
                      tempVal = charVal * 2;
                        if(tempVal > 9) tempVal = tempVal - 9;
                           checkSum = checkSum + tempVal;
                      }
                else {
                flipflop = true;
                checkSum = checkSum + (charVal);
                }
              }
              if(checkSum < 1) {
                 isValid = false;
                 return;
                 }
              checkSum = checkSum + ckDigit;
              checkSum = checkSum % 10;
              if(checkSum == 0)
              isValid = true;
              else
              isValid = false;
            }

            public static void main(String[] args) {
                CreditCard cc = new CreditCard("0000 0000 0000 0000");
                boolean test = cc.validate();
                System.out.println(test);
            }
        }


Home