Source Code: Credit card number validation (Israeli Visa)

Sometimes, you need more then the simple straight forward validation of a credit card number.

Usually, it’s enough, yet sometimes a client demand or a product demand is to create a more sophisticated validation, using known credit card algorithm the CreditCard company issues from time to time.

I had just that need a while back.

So, I created the validation and I decided to share it with you here.

First, let’s create a helper for checking wether a number is an even or an odd number. this will be used later in the validation process.
A credit card validation is being done using something that’s called a “weight number”, each credit card number is attached to a weight number and some calculations are done accordingly. So, let’s create a class which takes a number and a weight number, we will also create a constructor for this class.

Now, we have the helper to check whether a number is odd or even, we have a class to hold the credit card numbers.

the algorithm says something simple, starting at the right side of the number (credit card number) start attaching weight numbers.

start with 1, then 2 and so on and so forth till the end of the number.

like so:

Numbers and weight numbers

After you do this, multiply the number with the weight number, if the result you get is greated then the number 10, add the first number to the second number.

Example: if you get 16 in the result, simply add 1+6 and the final result is 7.

like so:

Calculation metod - weight numbers

After you do this, simply sum up the result

8+5+7+0+2+4+0+0+0+1+4+1+6+2+2+8

Any result should is OK as long as the number is divided by 10, if the number is not dived by 10 exactly something is wrong with the credit card.

This is the final validation function

The function is commented so no further explanation is needed.

Feel free to ask any question in the comments

Avi Tzurel

My name is Avi Tzurel. I'm a professional web developer from Israel. I spend most of my day developing both web products and RIA applications as well as imparting my experience onto others. I speak, teach and write about my passions, and develop applications according to what I preach. I specialize in Flex, Adobe Air, HTML, XHTML, Javascript, jQuery and other Javascript libraries, on the server side I do .net along side with Ruby on Rails. You can connect with me on Twitter or email me through the contact page on this blog.

Posted Monday, January 18th, 2010 under General, asp.net.

5 comments

  1. Hi,

    Nice post, just an idea, you can also identify if the card is VISA / Mastercard etc by the first 4 digits of the card number ( I am talking from memory here from years ago when I wrote something similar )

    You can in theory ( if you can find a list ) also determine which bank the user is with, so this can also be good for another level of validation.

    Cheers,

    Chris

  2. @Chris: Hi Chris,
    Sure, you can do a validation on the credit card 4 first digits.

    There is no need (in this specific case) to do a validation on the bank so I excluded this out of the validation.

    Thanks for your comment

  3. i am not a fan of having credits and getting credits cards..’~

  4. Hi,

    Nice post, just an idea, you can also identify if the card is VISA / Mastercard etc by the first 4 digits of the card number ( I am talking from memory here from years ago when I wrote something similar )

    You can in theory ( if you can find a list ) also determine which bank the user is with, so this can also be good for another level of validation.

    Cheers,

    Chris

Leave a Reply