Reputation: 23
I am writing code for entering credit cards into a database. I am aware there are regular expressions that can be used to determine type of card (Visa, MasterCard, etc). For instance, Visa's regex is /^4[0-9]{12}(?:[0-9]{3})?$/
. I only deal with Visa, MasterCard, Discover Card, and American Express, with no other cards being supported.
My question is: Should I collect the card type from the user and store it in the database, or derive the card type from the card number, and not store it in the database? In other words, are there any cases where, for instance, a Visa card will not match the regex, but still be a Visa card?
This is not about Luhn checking, it's about regular expressions for determining card type.
Upvotes: 2
Views: 182
Reputation: 175748
are there any cases where, for instance, a Visa card will not match the regex, but still be a Visa card?
Yes, that Visa expression limits to 13 or 16 digits when in reality Visa can range from 13 thru 19 digits, albeit that 16 is by far the most common.
Should I collect the card type from the user
This is discussed in depth here: Why do credit card forms ask for Visa, MasterCard, etc.?
As you comment you are experiencing problems with incorrect user input, I would simply not ask them what they are using and use a set of simple prefix and length regular expressions to identity the type of the card. You can then store this value in your database to save deriving it again the the future for query purposes).
Upvotes: 1