[C#] 一个用于信用卡卡号校验的C#类 →→→→→进入此内容的聊天室

来自 , 2020-09-07, 写在 C#, 查看 154 次.
URL http://www.code666.cn/view/d26beb4d
  1. using System;
  2.  
  3. namespace CreditCards
  4. {
  5.     /// <summary>
  6.     /// Summary description for CreditCardValidator.
  7.     /// </summary>
  8.     public class CreditCardValidator
  9.     {
  10.         public CreditCardValidator()
  11.         {
  12.             //
  13.             // TODO: Add constructor logic here
  14.             //
  15.         }
  16.  
  17.         public bool ValidateCC(string creditNo)
  18.         {
  19.             int indicator = 1;        //-- will be indicator for every other number
  20.             int firstNumToAdd = 0;  //-- will be used to store sum of first set of numbers
  21.             int secondNumToAdd = 0; //-- will be used to store second set of numbers
  22.             string num1;            //-- will be used if every other number added is greater than 10, store the left-most integer here
  23.             string num2;            //-- will be used if ever yother number added is greater than 10, store the right-most integer here
  24.    
  25.             //-- Convert our creditNo string to a char array
  26.             char[] ccArr = creditNo.ToCharArray();
  27.    
  28.             for (int i=ccArr.Length-1;i>=0;i--)
  29.             {
  30.                 char ccNoAdd = ccArr[i];
  31.                 int ccAdd = Int32.Parse(ccNoAdd.ToString());
  32.                 if (indicator == 1)
  33.                 {
  34.                     //-- If we are on the odd number of numbers, add that number to our total
  35.                     firstNumToAdd += ccAdd;
  36.                     //-- set our indicator to 0 so that our code will know to skip to the next piece
  37.                     indicator = 0;
  38.                 }
  39.                 else
  40.                 {
  41.                     //-- if the current integer doubled is greater than 10
  42.                     //-- split the sum in to two integers and add them together
  43.                     //-- we then add it to our total here
  44.                     if ((ccAdd + ccAdd) >= 10)
  45.                     {
  46.                         int temporary = (ccAdd + ccAdd);
  47.                         num1 = temporary.ToString().Substring(0,1);
  48.                         num2 = temporary.ToString().Substring(1,1);
  49.                         secondNumToAdd += (Convert.ToInt32(num1) + Convert.ToInt32(num2));
  50.                     }
  51.                     else
  52.                     {
  53.                         //-- otherwise, just add them together and add them to our total
  54.                         secondNumToAdd += ccAdd + ccAdd;
  55.                     }
  56.                     //-- set our indicator to 1 so for the next integer we will perform a different set of code
  57.                     indicator = 1;
  58.                 }
  59.             }
  60.             //-- If the sum of our 2 numbers is divisible by 10, then the card is valid. Otherwise, it is not
  61.             bool isValid = false;
  62.             if ((firstNumToAdd + secondNumToAdd) % 10 == 0)
  63.             {
  64.                 isValid = true;
  65.             }
  66.             else
  67.             {
  68.                 isValid = false;
  69.             }
  70.             return isValid;
  71.         }
  72.  
  73.         /// <summary>
  74.         /// Get and check to make sure that the chosen credit card type matches the
  75.         /// card number
  76.         /// </summary>
  77.         /// <param name="CCNum">string</param>
  78.         /// <returns>bool</returns>
  79.         public bool RetrieveCCType(string CCNum, int intChosenType)
  80.         {
  81.             // return value
  82.             bool bResult = false;
  83.  
  84.             if (Convert.ToInt32(CCNum.Substring(0,2)) >= 51 && Convert.ToInt32(CCNum.Substring(0,2)) <= 55)
  85.             {
  86.                 // Check to see that the type of card that the user selected
  87.                 // matches the
  88.                 if(intChosenType == 3)
  89.                 {
  90.                     bResult = true;
  91.                 }
  92.                 else
  93.                 {
  94.                     bResult = false;
  95.                 }
  96.             }
  97.             else if (Convert.ToString(CCNum.Substring(0,1)) == "4")
  98.             {
  99.                 // Visa
  100.  
  101.                 if(intChosenType == 4)
  102.                 {
  103.                     bResult = true;
  104.                 }
  105.                 else
  106.                 {
  107.                     bResult = false;
  108.                 }
  109.             }
  110.             else if (Convert.ToString(CCNum.Substring(0,2)) == "34" || Convert.ToString(CCNum.Substring(0,2)) == "37")
  111.             {
  112.                 // american express
  113.                 // check to see that the card matches the type they chose
  114.                 if(intChosenType == 1)
  115.                 {
  116.                     bResult = true;
  117.                 }
  118.                 else
  119.                 {
  120.                     bResult = false;
  121.                 }
  122.             }
  123.             else if(Convert.ToString(CCNum.Substring(0,4)) == "6011")
  124.             {
  125.                 // Discover card
  126.                 if(intChosenType == 2)
  127.                 {
  128.                     bResult = true;
  129.                 }
  130.                 else
  131.                 {
  132.                     bResult = false;
  133.                 }
  134.             }
  135.             else
  136.             {
  137.                 bResult = false;
  138.             }
  139.    
  140.             return bResult;
  141.         }
  142.     }
  143. }
  144. //csharp/7099

回复 "一个用于信用卡卡号校验的C#类"

这儿你可以回复上面这条便签

captcha