using System;

 

namespace prjstart

{

      

 

/* Credit card validation is done by using Luhns formula which goes in this fashion

        * Step 1: Double the value of alternate digits of the primary account number beginning with the second digit from the right (the first right--hand digit is the check digit.)

 

Step 2: Add the individual digits comprising the products obtained in Step 1 to each of the unaffected digits in the original number.

 

Step 3: The total obtained in Step 2 must be a number ending in zero (30, 40, 50, etc.) for the account number to be validated.

 

For example, to validate the primary account number 49927398716:

 

Step 1:

 

        4 9 9 2 7 3 9 8 7 1 6

         x2  x2  x2  x2  x2

------------------------------

         18   4   6  16   2

 

 

 

 

 

Step 2: 4 +(1+8)+ 9 + (4) + 7 + (6) + 9 +(1+6) + 7 + (2) + 6

 

Step 3: Sum = 70 : Card number is validated

 

Note: Card is valid because the 70/10 yields no remainder.

 

 

        * I have implemented the logic in C# ...Read on.........

        *

        *

        *

        *

        * */

      

      

      

      

       /// <summary>

       /// Summary description for Creditcardvalidation.

       /// </summary>

       public class Creditcardvalidation

       {

              //public enum cardtype

              //{

              //    MasterCard,Visa,AmericanExpress

              //};

              public string validcardtype;

 

              public string cardtype

              {

                     get

                     {

                           return validcardtype;

                     }

                     set

                     {

                        validcardtype = value;

                     }

              }

 

 

 

 

 

              public bool validate(string crd,string cardnumber)

              {

                     byte[] number = new byte[16];

                     int len=0;

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

                     {

                            if(char.IsDigit(cardnumber,i))

                           {

                                  if(len==16)return false;

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

                           }

 

                          

                     }

 

                     switch(crd)

                     {

                           case "MasterCard"   :

                                  if(len!=16)

                                         return false;

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

                                         return false;

                                  break;

 

                           case "AmericanExpress" :

                                  if(len!=15)

                                         return false;

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

                                         return false;

                                  break;

 

                           case "Visa"     :

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

                                         return false;

                                  if(number[0] != 4)

                                         return false;

                                  break;

                     }

 

                    

                     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];

                     }

                     if(sum == 0)

                     {

                           return false;

 

                     }

                     else

                     {

                           return (sum % 10 == 0);

                     }

 

 

 

                    

 

                    

             

             

              }

 

 

       }

}

 

 

Back to Main page