- The first Regular Expression Library on the Web!

Please support RegExLib Sponsors


Regular Expression Details

Title Test Find Pattern Title
^(?:(?<Visa>4\d{3})|(?<Mastercard>5[1-5]\d{2})|(?<Discover>6011)|(?<DinersClub>(?:3[68]\d{2})|(?:30[0-5]\d))|(?<AmericanExpress>3[47]\d{2}))([ -]?)(?(DinersClub)(?:\d{6}\1\d{4})|(?(AmericanExpress)(?:\d{6}\1\d{5})|(?:\d{4}\1\d{4}\1\d{4})))$
Updated on 7 Jun 2005 -- Matches major credit cards including: Visa (length 16, prefix 4); Mastercard (length 16, prefix 51-55); Diners Club/Carte Blanche (length 14, prefix 36, 38, or 300-305); Discover (length 16, prefix 6011); American Express (length 15, prefix 34 or 37). Saves the card type as a named group to facilitate further validation against a &quot;card type&quot; checkbox in a program. All 16 digit formats are grouped 4-4-4-4 with an optional hyphen or space between each group of 4 digits. The American Express format is grouped 4-6-5 with an optional hyphen or space between each group of digits. Formatting characters must be consistant, i.e. if two groups are separated by a hyphen, all groups must be separated by a hyphen for a match to occur.
4111-2222-3333-4444 | 3411 222222 33333 | 5111222233334444
4111-2222-3333-444 | 3411-2222-3333-4444 | Visa
Author Rating: Not yet rated. Jerry Schmersahl
Your Rating
Bad Good

Enter New Comment

Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

Title: Not working
Name: JN
Date: 12/31/2007 5:51:44 PM
Regular expressions are supported with most versin of GNU Unix tools, but I have yet to find one that will accept this expression. The error I get from most of them is "Invalid back reference" I've tried grep -e <stuff supplied above> <filename> as well as grep -e "<stuff supplied above>" <filename> and have had zero success.

Title: Not working
Name: R.Abarca
Date: 6/7/2005 7:05:29 PM
I've tested in JS with IE 6 and it reports there's an error in the syntax.

Title: Not for JavaScript
Name: Kristen
Date: 4/21/2004 9:31:37 AM
Doesn't work with JavaScript (IE6)

Copyright © 2001-2021, | ASP.NET Tutorials