Search  Search    
    
News, IT news, free tutorials, photos, graphics, Flash advertising solutions, logos design, banners design, free e-books, free icons, wallpapers, CD presentation, DVD presentation, Web design.
How to test Credit Cards validity in C#. Sample code.
  More Windows tutorials ... 

 

Complete guide for Euro 2012’, about 300 hundred pages filled with comprehensive information about Euro 2012


Euro 12 - teams, managers, players, fixtures, referees, the brand new ‘Tango 12’ Adidas ball and much, much more. Few hundreds amazing photos.


E-books, free e-books, Euro 2012 e-books


Auckland events in photos at bonanz.com


1 500 photos of fun-run 'Ports of Auckland Round the Bays 2012' at bonanz.com


Ocean swim - more than 1 500 photos of State King of the Bays

 

SoftFern.com Forums BonaNZ.com forums

 

SoftFern.com Forums - articles, news, links   

 

BonaNZ.com Forums - articles, news, links   


Photos of State King of the Bays event at bonanz.com


‘Complete guide for Euro 2012’: Given profile all 368 footballers with their photos.
 
 cWeb design, Software development, Graphics, Design Logo, Banners, DVD/CD presentation
 


‘Complete guide for Euro 2012’: Read about star players of each squad. Find out who is the key player of each team. What Pele expects of Euro 2012?



‘Complete guide for Euro 2012’: Compare annual salary of coaches participating in the Euro-2012 - and how it happened that the team of the most paid coach finished the last in their group?




‘Complete guide for Euro 2012’: Euro 2012 footballers: who are the most expensive players? Who are the Rising stars at Euro 2012?





E-books, free e-books, Euro 2012 e-books
 
  
 Popularity 
 Rating: 3.8      Rate this article   60 
 26 May 2012

How to test Credit Cards validity in C#. Sample code.

 







How to test Credit Cards validity in C#. Sample code.

Below is class to check validity of Credit Cards. It can be used at Checkout web page, when a customer supplied his credit card information.

It is written in C# for .NET version 2.0 Framework. It can validate MasterCard, BankCard, Visa, AmericanExpress, Discover, DinersClub, JCB Credit Cards.

To use the code, add to Checkout web page drop down box with the list of credit cards you accept, add text box for Credit Card Number, text box for Name on Credit Card, drop down lists for Credit Card expiry dates: month and year.

When user entered his Credit Card data, you need to verify the validity of the Credit Card number.
The code can check credit card number in any format: 5111111111111118 or 5111-1111-1111-1118
or 5111 1111 1111 1118.


How to use the class. The sample code from Checkout web page:
 

protected bool TestEntryCrCard()

        {

            bool bRetVAl = false;

            try

            {

                string strCardNumber = CardNumber.Text.Trim();

                CardValidator.CardType oCrdType = CardValidator.CardType.Visa;

 

                switch ( int.Parse (drpCC.SelectedValue.ToString()) )

                {

                    case 1:

                        oCrdType = CardValidator.CardType.Visa;

                         break;

 

                    case 2:

                        oCrdType = CardValidator.CardType.MasterCard;

                        break;

 

                    case 3:

                        oCrdType = CardValidator.CardType.AmericanExpress;

                        break;

 

                    case 4:

                        oCrdType = CardValidator.CardType.DinersClub;

                        break;

                    

                }// switch ( int.

 

                if (!CardValidator.Validate(oCrdType, strCardNumber))

                {

                    // Write code to show to the customer that the Credit Card is invalid

                }

                 else

                {

                     // Credit card is valid

                     bRetVAl = true;

    }

                        

            }

            catch (Exception ex){}

                         

            return bRetVAl;

        }

 

 


Class to validate Credit Cards:

 

namespace SoftFern_com

{

 

    public class CardValidator

    {

        public enum CardType

        {

            MasterCard, BankCard, Visa, AmericanExpress, Discover, DinersClub, JCB

        }

 

        public static bool Validate(CardType cardType, string cardNumber)

        {

            byte[] number = new byte[16]; // number to validate

 

            // To remove non-digits

            int len = 0;

           

            for (int i = 0; i < cardNumber.Length; i++)

            {

                if (char.IsDigit(cardNumber, i))

                {

                    if (len == 16)

                    {

                        return false; // number has too many digits

                    }

 

                    number[len++] = byte.Parse( cardNumber[i].ToString());

                }//if (char.IsDigit(cardNumber, i))

            }

 

 

            // Validate based on card type, at first if tests length, then it tests prefix

      switch(cardType)

      {

         case CardType.MasterCard:

             if (len != 16)

             {

                 return false;

             }

 

             if (number[0] != 5 || number[1] == 0 || number[1] > 5)

             {

                 return false;

             }

              

            break;

 

         case CardType.BankCard:

             if (len != 16)

             {

                 return false;

             }

 

             if (number[0] != 5 || number[1] != 6 || number[2] > 1)

             {

                 return false;

             }

              

            break;

 

         case CardType.Visa:

             if (len != 16 && len != 13)

             {

                 return false;

             }

 

             if (number[0] != 4)

             {

                 return false;

             }

              

            break;

 

         case CardType.AmericanExpress:

             if (len != 15)

             {

                 return false;

             }

              

             

            if(number[0] != 3 || (number[1] != 4 && number[1] != 7))

            {

                return false;

            }

 

            break;

 

         case CardType.Discover:

            if(len != 16)

            {

                return false;

            }

 

            if(number[0] != 6 || number[1] != 0 || number[2] != 1 || number[3] != 1)

            {

                return false;

            }

 

            break;

 

         case CardType.DinersClub:

            if(len != 14)

            {

                return false;

            }

 

             

            if(number[0] != 3 || (number[1] != 0 && number[1] != 6 && number[1] != 8)

               || number[1] == 0 && number[2] > 5)

            {

                return false;

            }

 

            break;

 

         case CardType.JCB:

              //if(len != 16 &amp;&amp; len != 15)

            if(len != 16 && len != 15)

            {

                return false;

            }

 

            if(number[0] != 3 || number[1] != 5)

            {

                return false;

            }

 

            break;

      }//switch(cardType)

 

        // Use Luhn Algorithm to validate

      int sum = 0;

     

            for(int i = len - 1; i >= 0; i--)

              {

                  if (i % 2 == len % 2)

                  {

                      int n = number[i] * 2;

                      sum += (n / 10) + (n % 10);

                  }

                  else

                  {

                      sum += number[i];

                  }

              }//for(int i = len - 1; i >= 0; i--)

 

 

            return (sum % 10 == 0);

        }//func

 

    }//class

}

 

In our next article we will provide samples of credit card numbers for testing credit card functionality.

 
















More similar topics with SQL tips:

Alter Table, Alter Column in MS SQL Server 2008

GUID formats and length.

Sparse Columns in SQL Server 2008

Oracle database - How to select several random records from a table

Database DB2 - How to select several random records from a table

MySQL database - How to select several random records from a table

PostgreSQL - How to select several random records from a table

Microsoft SQL Server - How to select several random records from a table




GUID formats and length.

How to hide (disable) ControlBox?

How to test string for an Integer?

How to test string for a number?

Drag and drop a file in C# - easy

Adding and Remowing Connections to Microsoft SQL server

MS SQL server - connections and Connection Pooling

Usage of Unsafe Code in C#

Pointers Basics

Unsafe Code Usage in C#

Unsafe Code in C#
  
  More similar links:

Drag and drop a file in C# - easy.

Pointers Basics

Sample Credit Card Numbers for Testing Credit Card Functionality

Unsafe Code Usage in C#

Hardware Requirements for Microsoft Visual Studio 2010

Usage of Unsafe Code in C#

How to hide  (disable)  ControlBox?

 
    
 Rate this article from 1 to 5 

      Comments
         
 

11.07.2012    Aufa

You've really helped me understand the isseus. Thanks.


11.07.2012    Aabb

Gosh, I wish I would have had that information eialrer!


12.07.2012    wnjaczjzr

MXEi5v , [url=http://zaggyvlpciph.com/]zaggyvlpciph[/url], [link=http://pmcdimijkoce.com/]pmcdimijkoce[/link], http://uxenngktustv.com/


13.07.2012    jbhhpsftnrb

iPLSok , [url=http://bppejzokmrac.com/]bppejzokmrac[/url], [link=http://sgirtvxwohhr.com/]sgirtvxwohhr[/link], http://dakkgwhxtyrd.com/


14.07.2012    oumycejcq

edDN5W , [url=http://ogdzycamwuga.com/]ogdzycamwuga[/url], [link=http://wpgzbmdvtrha.com/]wpgzbmdvtrha[/link], http://ulefwmuguezh.com/


14.07.2012    wxlymkick

tlvKZn , [url=http://vcbgnprsykne.com/]vcbgnprsykne[/url], [link=http://bcsxlzjofzpe.com/]bcsxlzjofzpe[/link], http://iacshdjehhqo.com/

         
        Add comment
Your name:
Your comment:
                      
 Tags: C#, Unsafe, Code, pointers, SoftFern Tutorials, SoftFern Programming Tutorials, memory management, Credit Cards validity, Credit Cards validity in C#, code to test Credit Cards validity , C# code, Credit Card Number, to check Credit Card Number, to check valid Credit Card Number, check valid Credit Card Number in C
   More Windows tutorials ...
News, IT news, free tutorials, photos, graphics, Flash advertising solutions, logos design, banners design, free e-books, free icons, wallpapers, CD presentation, DVD presentation, Web design.  
Home - News  - Tech news - News football - Sport news - Health & Beauty - Misc news
Photoshop and Photography tutorials - iPad, Androids, Tablet PCs - Programming - Windows
- Microsoft Office - Misc tutorials- Site Map Control Panel - Site Map Grid
- Add Link - Links Exchange - Our Portfolio - Samples web sites   Archive of news and tutorials

SoftFern (New Zealand LTD). 
Design and development by SoftFern 2003 - 2010 Graphics supplied by BonaNZ.com