WDX C# 信用卡验证功能

分享于 

8分钟阅读

Web开发

  繁體

WDX - C# Credit Card Valudation Functions

介绍

以下是我在 C# 中进行基本信用/借记卡验证检查的付款类。 它包括以下功能:

  • LUHN检查
  • 卡片类型查找

背景

LUHN检查是从另一个例子中获取的,但我不记得在哪里给我信用卡,如果你在它的他地方。

卡片类型查找相当简单。 它遵循维基百科的一些简单规则来识别卡片类型。 这些并不是全面的,并且不总是有效的,因为没有定义的标准。

使用代码

这个类包含两个 static 方法,可以从例程调用它们来帮助验证,所有你需要做的就是通过卡号。

LUHN检查


public static bool IsLUHNValid(string ccNo)


{


 bool isValid = false;


 // will be indicator for every other number


 int indicator = 1;


 // will be used to store sum of first set of numbers


 int firstNumToAdd = 0;


 // will be used to store second set of numbers


 int secondNumToAdd = 0;


 // will be used if every other number added


 // is greater than 10, store the left-most integer here


 string num1;


 // will be used if ever yother number added


 // is greater than 10, store the right-most integer here


 string num2;



 // Convert our creditNo string to a char array


 char[] ccArr = ccNo.ToCharArray();



 if (IsNumeric(ccNo))


 {


 for (int i=ccArr.Length-1;i>=0;i--)


 {


 char ccNoAdd = ccArr[i];


 int ccAdd = Int32.Parse(ccNoAdd.ToString());


 if (indicator == 1)


 {


 // If we are on the odd number of numbers,


 // add that number to our total


 firstNumToAdd += ccAdd;


 // set our indicator to 0 so that our code


 // will know to skip to the next piece


 indicator = 0;


 }


 else


 {


 // if the current integer doubled is greater than 10


 // split the sum in to two integers and add them together


 // we then add it to our total here


 if ((ccAdd + ccAdd) >= 10)


 {


 int temporary = (ccAdd + ccAdd);


 num1 = temporary.ToString().Substring(0,1);


 num2 = temporary.ToString().Substring(1,1);


 secondNumToAdd += (Convert.ToInt32(num1) + 


 Convert.ToInt32(num2));


 }


 else


 {


 // otherwise, just add them together


 // and add them to our total


 secondNumToAdd += ccAdd + ccAdd;


 }


 // set our indicator to 1 so for the next integer


 // we will perform a different set of code


 indicator = 1;


 }


 }


 // If the sum of our 2 numbers is divisible by 10,


 // then the card is valid. Otherwise, it is not


 if ((firstNumToAdd + secondNumToAdd) % 10 == 0)


 {


 isValid = true;


 }


 else


 {


 isValid = false;


 }


 }


 else


 isValid = false;



 return isValid;


}



获取卡类型


public enum CardType


{


 Invalid,


 Unknown,


 AmericanExpress,


 Bankcard,


 DinersClubInternational,


 DinersClubUSandCanada,


 DiscoverCard,


 JCB,


 Maestro,


 MasterCard,


 SoloDebit,


 SwitchDebit,


 Visa,


 VisaElectron,


 enRoute


}



public static CardType GetCardType(string strCardNumber)


{


 strCardNumber = strCardNumber.Replace("", "");



 // Check American Express


 if (strCardNumber.Substring(0, 2)=="34" || 


 strCardNumber.Substring(0, 2)=="37")


 {


 if (strCardNumber.Length==15)


 return CardType.AmericanExpress;


 else


 return CardType.Invalid;


 }



 // Check Bankcard


 if (strCardNumber.Substring(0, 3).ToUpper() == "DNE")


 return CardType.Bankcard;



 // Check Diners Club Internationl


 if (strCardNumber.Substring(0, 2) == "36" || 


 strCardNumber.Substring(0, 2) == "38" || 


 (Convert.ToInt32(strCardNumber.Substring(0, 3)) >= 300 && 


 Convert.ToInt32(strCardNumber.Substring(0, 3)) <= 305))



 if (strCardNumber.Length==14)


 return CardType.DinersClubInternational;


 else


 return CardType.Invalid;



 // Check Diners Club US and Canadaa


 if (strCardNumber.Substring(0, 2) == "55")


 return CardType.DinersClubUSandCanada;



 // Check Discover Card


 if (strCardNumber.Substring(0, 4) == "6011") 


 if (strCardNumber.Length==16)


 return CardType.Bankcard;


 else


 return CardType.Invalid;



 // Check JCB Card


 if (strCardNumber.Substring(0, 4) == "2131" || 


 strCardNumber.Substring(0, 4) == "1800" ) 


 if (strCardNumber.Length==15)


 return CardType.JCB;


 else


 return CardType.Invalid;



 // Check Maestro Card


 if (strCardNumber.Substring(0, 4) == "5020") 


 if (strCardNumber.Length==16)


 return CardType.Maestro;


 else


 return CardType.Invalid;



 // Check MasterCard


 if ((Convert.ToInt32(strCardNumber.Substring(0, 3)) >= 51 && 


 Convert.ToInt32(strCardNumber.Substring(0, 3)) <= 55))



 if (strCardNumber.Length==16)


 return CardType.MasterCard;


 else


 return CardType.Invalid;



 // Check Solo Card


 if (strCardNumber.Substring(0, 2) == "63" || 


 strCardNumber.Substring(0, 4) == "6767") 


 if (strCardNumber.Length==16 || strCardNumber.Length==18 ||


 strCardNumber.Length==19)


 return CardType.SoloDebit;


 else


 return CardType.Invalid;



 // Check Switch Card


 if (strCardNumber.Substring(0, 4) == "4903" || 


 strCardNumber.Substring(0, 4)== "4905" || 


 strCardNumber.Substring(0, 4) == "4911" || 


 strCardNumber.Substring(0, 4) == "4936" || 


 strCardNumber.Substring(0, 6) == "564182" || 


 strCardNumber.Substring(0, 6) == "633110" || 


 strCardNumber.Substring(0, 4) == "6333" || 


 strCardNumber.Substring(0, 4) == "6759") 


 if (strCardNumber.Length==16 || strCardNumber.Length==18 || 


 strCardNumber.Length==19)


 return CardType.SwitchDebit;


 else


 return CardType.Invalid;



 // Check Visa Electron Card


 if (strCardNumber.Substring(0, 6) == "417500") 


 if (strCardNumber.Length==16)


 return CardType.VisaElectron;


 else


 return CardType.Invalid;



 // Check Visa Card


 if (strCardNumber.Substring(0, 1) == "4") 


 if (strCardNumber.Length==16)


 return CardType.Visa;


 else


 return CardType.Invalid;



 // Check enRoute Card


 if (strCardNumber.Substring(0, 4) == "2014" || 


 strCardNumber.Substring(0, 4) == "2149") 


 if (strCardNumber.Length==15)


 return CardType.enRoute;


 else


 return CardType.Invalid;



 return CardType.Unknown;


}



历史记录

  • 1.0 - 22 2006年08月 - 检查在这里日期上在维基百科上指示的前缀。

FUNC  函数  CAR  functions  信用卡  
相关文章