Click here to Skip to main content
15,884,099 members
Articles / Web Development / ASP.NET

Ultimate .NET Credit Card Utility Class

Rate me:
Please Sign up or sign in to vote.
4.84/5 (41 votes)
29 Aug 2007CPOL6 min read 196.8K   6.4K   161  
A powerfully simple .NET utility class for validating and testing credit card numbers in C# and VB.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Bluelaser.Utilities;
using System.Text;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //Display test credit card numbers
            this.lblMc.Text = CreditCardUtility.GetCardTestNumber(CreditCardTypeType.MasterCard).Replace(" ", "-");
            this.lblVisa.Text = CreditCardUtility.GetCardTestNumber(CreditCardTypeType.Visa).Replace(" ", "-");
            this.lblDisc.Text = CreditCardUtility.GetCardTestNumber(CreditCardTypeType.Discover).Replace(" ", "-");
            this.lblAmex.Text = CreditCardUtility.GetCardTestNumber(CreditCardTypeType.Amex).Replace(" ", "-");
        }
    }

    protected void btnValidate_Click(object sender, EventArgs e)
    {
        if (txtCard.Text.Length > 0)
        {
            string cardNum = txtCard.Text.Trim();

            if (CreditCardUtility.IsValidNumber(cardNum))
            {
                CreditCardTypeType? cardType = CreditCardUtility.GetCardTypeFromNumber(cardNum);
                string strCardType = (cardType == null) ? "Unknown" : cardType.ToString();

                showMessage(String.Format("You have entered a valid card number. The card type is {0}.", strCardType), true);
            }
            else
                showMessage("Card failed Luhn test. Please enter a valid card number.", false);
        }
        else
            showMessage("Please enter a card number first.", false);
    }

    protected void showMessage(string msg, bool isSuccess)
    {
        this.lblMsg.Text = msg;

        if (isSuccess)
            lblMsg.CssClass = "success";
        else
            lblMsg.CssClass = "fail";
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Web Developer
United States United States
Todd Anglin is an Experts Exchange ASP.NET Master and a Telerik Technical Evangelist responsible for building a strong Telerik community across the globe. Before joining Telerik, Todd worked as a developer in a Fortune 200 financial services company in San Antonio as a Systems Analyst supporting applications on a wide range of platforms and technologies, including Unix, Windows Server, Informix, Oracle, and SQL Server. Todd graduated Magna Cum Laude with Business Honors from Mays Business School at Texas A&M University with a BBA in MIS and now resides with his wife in The Woodlands, Texas.

Comments and Discussions