Click here to Skip to main content
15,921,990 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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#
 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.
Posted
Updated 2-Dec-19 21:57pm
Comments
CodeProjectWeb 3-Dec-19 3:57am    
Hi,
I want to get same solution but in knockout and also card type based on token.
so can you help me out
Dave Kreskowiak 3-Dec-19 11:51am    
You didn't even bother to look as the solutions listed on this page before posting, did you?

 
Share this answer
 
Comments
[no name] 14-Feb-14 5:51am    
Thank you got it solved..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900