Validate Credit Card Numbers with Regular Expressions

Expressions below account for IIN numbers on CC’s of most major US issuers which includes validating starting character. These should be used in addition to Luhn Algorithm mod10 check.

These expressions can be used on clientside and codebehind to give your end users notification of invalid entry without wasting resources on a failed/invalid post. See snippet below. Expressions should technically be cross-platform.

Note: although very similar to those found on regular-expressions.info, there are some small differences which do not account for the old standards in my examples since these cards are no longer in circulation.

(C#)

string vs = @"((^4)[0-9]{15}$)|";
            string mc = @"((^5[1-5])[0-9]{14}$)|";
            string ax = @"((^3[4|7])[0-9]{14})$|";
            string ds = @"(^6(011|5[0-9]{2})[0-9]{12}$)";

string expirationmonth = @"((^[0-9]$)|(^[0-9][0-2]$))";

string ordertotalamount = @"((^[0-9]{1,5}$)|(^[0-9]{1,5}\.[0-9]{1,2}$))"; //does not account for currency symbols

System.Text.StringBuilder sbexp = new System.Text.StringBuilder();
            for (int i = 0; i <= 20; i++) //create list of years from now+20. same range amazon uses for card
            {
                sbexp.Append("(^" + (DateTime.Now.Year + i).ToString() + "$)|");
                if (i < 20) //don't append or on last
                {
                    sbexp.Append((DateTime.Now.Year + i).ToString());
                }
            }

            ((RegularExpressionValidator)validator).ValidationExpression = sbexp.ToString();

((RegularExpressionValidator)validator).ValidationExpression = vs+mc+ax+ds;

References
Wikipedia (IIN numbers), http://en.wikipedia.org/wiki/Bank_card_number
Wikipedia (Luhn Algorithm), http://en.wikipedia.org/wiki/Luhn_algorithm
RegularExpressions.info, http://www.regular-expressions.info/creditcard.html

Advertisement

About Ronnie Diaz

Ronnie Diaz is a software engineer and tech consultant. Ronnie started his career in front-end and back-end development for companies in ecommerce, service industries and remote education. This work transitioned from traditional desktop client-server applications through early cloud development. Software included human resource management and service technician workflows, online retail e-commerce and electronic ordering and fulfillment, IVR customer relational systems, and video streaming remote learning SCORM web applications. Hands on server experience and software performance optimization led to creation of a startup business focused on collocated data center services and continued experience with video streaming hardware and software. This led to a career in Amazon Prime Video where Ronnie is currently employed, building software and systems which stream live sports and events for millions of viewers around the world.

Posted on October 31, 2011, in Programming & Development and tagged , , , , , , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: