Our services: SOFTWARE DEVELOPMENT,  WEB DEVELOPMENT, DATABASE DESIGN and ADMINISTRATION, ENHANCEMENT of EXISTING SOFTWARE, ENHANCEMENT of EXISTING WEB SITES, ENHANCEMENT of EXISTING DATABASE, GRAPHIC DESIGN – LOGOS, BANNERS, ICONS…, PHOTOGRAPHY: SHOOTING PHOTOS, IMPROVING AND RETOUCHING, VIDEO PRODUCTION: RECORDING VIDEOS, CREATE MOVIES, PUBLISHING, SEO – SEARCH ENGINES OPTIMISATION, SOCIAL NETWORKS – POSTS ON TWITTER, GOOGLE+, FACEBOOK, LINKEDIN.

           Search  Search  

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

  SoftFern.com on Twitter   SoftFern.com on YouTube   SoftFern.com on flickr   SoftFern.com on Pinterest
SoftFern.com on Facebook   SoftFern.com on Google+   SoftFern.com on Tumblr   SoftFern.com on LinkedIn



Our services: SOFTWARE DEVELOPMENT,  WEB DEVELOPMENT, DATABASE DESIGN and ADMINISTRATION, ENHANCEMENT of EXISTING SOFTWARE, ENHANCEMENT of EXISTING WEB SITES, ENHANCEMENT of EXISTING DATABASE, GRAPHIC DESIGN – LOGOS, BANNERS, ICONS…, PHOTOGRAPHY: SHOOTING PHOTOS, IMPROVING AND RETOUCHING, VIDEO PRODUCTION: RECORDING VIDEOS, CREATE MOVIES, PUBLISHING, SEO – SEARCH ENGINES OPTIMISATION, SOCIAL NETWORKS – POSTS ON TWITTER, GOOGLE+, FACEBOOK, LINKEDIN.


Free wallpapers of the hottest girls of the 2014 Winter Olympics

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.
 
 


‘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.3      Rate this article   45 
 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:

How to copy folder with its permissions?

Hardware Requirements for Microsoft Visual Studio 2010

Unsafe Code Usage in C#

Pointers Basics

Sample Credit Card Numbers for Testing Credit Card Functionality

How to test string for an Integer?

How to fix HTTP Error 404.3 - Not Found (aspx file not recognized by IIS)

 
    
 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!

              
        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 Programming ...
 


  Home page Weird We sell Programming Windows
  News Our Photos Photoshop and Photography SQL server Microsoft Office
  Tech News Our Portfolio iPad Misc Web sites development
  Videos Archive Androids Graphic design Software development
  Auckland and New Zealand About Us Tablet PCs CD/DVD presentation Play games online
  Football Contact us Round the Bays 2012 photos Ocean Swim 2012 photos Auckland Events
  Sport Our Portfolio Stock Images Stock Photos Links Exchange
  Hot Girls Our clients On Twitter Add link Site Map
  Our Twitters’ gadget Site Map Google+ Site Grid Poklonskaya.info
  Health and Beauty Site Grid Pinterest Natalia Poklonskaya.info Soft Fern forums
  Misc News Web pages Facebook Photos, images Hair & Beauty
  On YouTube CD/DVD presentaton Tumblr Daily Motion Flickr
  YouTube - 2 Graphics LinkedIn Live Journal Stumble Upon
  Vimeo - videos Tutorials - Videos Tutorials - Weird Most Popular on Internet Yahts, boats, Tall Ships

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