Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

14. Validation, Formatting, and Regular ... > 14.7. Use Regular Expressions for Ma...

14.7. Use Regular Expressions for Matching Credit Card Numbers

Problem

You need a regular expression that will match the major credit card types: Visa, MasterCard, American Express, and Discover.

Solution

Create a regular expression that uses the initial digits of each major credit card type and match the expected number of digits for each type.

Discussion

The numbers of each major credit card type start with the same identifying digits, and you can use this fact to create a single regular expression for determining whether a card number is valid. All MasterCard card numbers start with 5, all Visa card numbers start with 4, all American Express card numbers start with 30, and all Discover card numbers start with 6011. The expression you need is as follows:


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint