Click here to Skip to main content
12,883,884 members (37,376 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 7 May 2005

Simple Mod 10 validation for creditcards

, 7 May 2005
Rate this:
Please Sign up or sign in to vote.
This article describes you an easy way of validating a credit card.


This article demonstrates a simple way of detecting errors while entering the credit card in a form. This method is popularly known as Mod 10 check. This saves a lot of time and money for ecommerce businesses by avoiding a trip to the banks. This program uses an Mod-10 Method (ISO 2894/ANSI 4.13) to verify the validity of a credit card number.

According to this method, starting from the last digit of the credit card number, multiply every second number by two. If the resulting number is greater than 9, substract 9 from that number to make it a single digit. Now add all the numbers and is taken modulo with 10. If the result is 0, then the credit card number is valid.

private bool ValidateCreditCardNumber(string creditCardNumber)
    //Replace any character other than 0-9 with ""
    creditCardNumber = Regex.Replace(creditCardNumber,@"[^0-9]","");
    int cardSize = creditCardNumber.Length;

    //Creditcard number length must be between 13 and 16
    if (cardSize >= 13 && cardSize <= 16)
       int odd = 0;
       int even = 0;
       char[] cardNumberArray = new char[cardSize];

       //Read the creditcard number into an array
       cardNumberArray = creditCardNumber.ToCharArray();

       //Reverse the array
       Array.Reverse(cardNumberArray, 0, cardSize);

       //Multiply every second number by two and get the sum. 
       //Get the sum of the rest of the numbers.
       for (int i = 0; i < cardSize; i++)
          if (i%2 ==0)
             odd += (Convert.ToInt32(cardNumberArray.GetValue(i))-48);   
             int temp = (Convert.ToInt32(cardNumberArray[i]) - 48) * 2;
             //if the value is greater than 9, substract 9 from the value
             if (temp > 9)
                temp = temp-9;
             even += temp;
        if ((odd+even)%10 == 0)     
           return true;
           return false;
         return false;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Prasad Papudesi
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionRegarding array.getvalue(i) - 48 Pin
kaibin873-Jul-06 12:31
memberkaibin873-Jul-06 12:31 
AnswerRe: Regarding array.getvalue(i) - 48 Pin
Oryan K7-Sep-06 15:00
memberOryan K7-Sep-06 15:00 
kaibin87 wrote:
Why is there a "- 48" there? this is the only part of the code which i didnt quite understand. I read thru the Introduction and comments but didnt found any evidence of anything stating there is a need to "- 48" for the credit card value. I tried testing out the code without the "- 48" but it didnt work correctly.

The -48 is there because GetValue() returns the ASCII value of the Character in the array. The ASCII value for "0" is 48 and "1" is 49 and so on.

- Oryan Smile | :)

QuestionVariable Card length ? Pin
mgmail17-Apr-06 10:46
membermgmail17-Apr-06 10:46 
AnswerRe: Variable Card length ? Pin
Oryan K7-Sep-06 15:07
memberOryan K7-Sep-06 15:07 
GeneralAnd now for the source code ..... Pin
fwsouthern10-May-05 16:02
memberfwsouthern10-May-05 16:02 
GeneralRe: And now for the source code ..... Pin
Chris Maunder10-May-05 16:37
adminChris Maunder10-May-05 16:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170422.1 | Last Updated 7 May 2005
Article Copyright 2005 by Prasad Papudesi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid