I am trying to get the credit card type when we give a credit card number i.e. when user gives a credit card number in a text box that input has to be taken validated using luhn check and get the type of card as AMEX, VISA etc.
I have gone through some examples in codeproject and tried to implement but i am unable to take the input into class created for different card types, i used regex and the code in c# is as below:
C#
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
int luhnCheckdigit = 0;
string cardNumber = TextBox1.Text.ToString();
luhnCheckdigit = checkSum(TextBox1.Text.ToString());
string TYPE = cardType(TextBox1.Text.ToString());
}
public enum cardType
{
unknown = 0,
MasterCard = 1,
VISA = 2,
AMEX = 3
}
public class CardtypeInfo
{
public CardtypeInfo(string regEx, int length, cardType type)
{
RegEx = regEx;
Length = length;
TYpe = type;
}
public string RegEx { get; set; }
public int Length { get; set; }
public cardType TYpe { get; set; }
}
private static CardtypeInfo[] _CardTypeInfo =
{
new CardtypeInfo("^(51|52|53|54|55)", 16, cardType.MasterCard),
new CardtypeInfo("^(4)", 16,cardType.VISA),
new CardtypeInfo("^(4)",13, cardType.VISA),
new CardtypeInfo("^(34|37)",15,cardType.AMEX),
};
public cardType GetcardType(string cardNumber)
{
foreach (CardtypeInfo info in _CardTypeInfo)
{
if (cardNumber.Length == info.Length && Regex.IsMatch(cardNumber, info.RegEx))
return info.TYpe;
}
return cardType.unknown;
}
please help me to get the input into the class cardtype and display the type of card when the text change event occured.