How to validate credit card numbers
The card number contains 16 digits, divided to 4 blocks for four digits each and serves as access to a bank account of a cardholder
Every credit card has a unique number. With the help of the first four or six numbers, you can find out an issuer bank identifier that payment system assigns to a particular bank for a specific type of card.
To identify whether a cardholder entered a valid number, you can use simple regular expressions.
Also, you can customize regular expressions and use them to find the necessary card or other appropriate numerical sequences.
This method also shows that you do not disclose the number of cardholders, but only identify them.
How to delete spaces?
When entering plastic card data, every four digits of the account number are separated by a space. It is used in order to reduce input errors or when reading. Thus, you can write an expression that allows us to remove excess characters to optimize the search.
First, we need to delete all characters that are not numbers (space is also a character and has a   HTML format). To solve this, let’s use the following function: [^0-9]+.
Let’s look at what each symbol means:
- [] – square brackets indicate that the search takes place over a specific range.
- ^ – this symbol means that the search will start from a new line.
- 0-9 – numbers from 0 to 9
- + – means the character that can be at the end of the expression. It is called a quantifier and determines how many times the expression can occur in the text. In this case, one or more. In this case, we can use the expression without a +, but it helps computers to save some calculation time.
If the task is to replace spaces or dashes, then you can find them in this way: [ -]+. In this case, according to the rules, the dash should be in front of the closing bracket.
Regular expressions are ideal for checks, such as finding and matching credit card numbers. Of all 16 characters, only a few are required for identification. On the one hand, this allows the issuer to identify the bank, and on the other hand, the full bank credit card number remains classified.
In practice, cards of different banks can be automatically sent to suitable payment processors, or inform the client that this type of card is not supported.
- For Visa card: ^4[0-9]{12}(?:[0-9]{3})?$. All Visa cards start with a digit four and are either 13-digit or 16-digit ones.
- MasterCard: ^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$. All Mastercard plastic cards contains 16 numbers and start with 5 or 2.
- American Express: ^3[47][0-9]{13}$. AMEX cards, as well as JCB International and Diners Club, starts at 3.
- Diners Club: ^3(?:0[0-5]|[68][0-9])[0-9]{11}$.
- Discover: ^6(?:011|5[0-9]{2})[0-9]{12}$. Discover, the same as Maestro and China UnionPay starts at 6.
- JCB: ^(?:2131|1800|35\d{3})\d{11}$. JCB cards also start at 2131, 1800.
To validate any of the cards mentioned above, the expressions can be combined into one using the symbol ‘|’ which denotes alternation construction.
A vertical bar ‘|’ can be used to match any set of patterns in which patterns are separated using the ‘|’ character.
^(?:4[0-9]{12}(?:[0-9]{3})? # Visa
| (?:5[1-5][0-9]{2} # MasterCard
| 222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}
| 3[47][0-9]{13} # American Express
| 3(?:0[0-5]|[68][0-9])[0-9]{11} # Diners Club
| 6(?:011|5[0-9]{2})[0-9]{12} # Discover
| (?:2131|1800|35\d{3})\d{11} # JCB
)$
If the client has entered the wrong number of characters or in the incorrect format, these formulas can quickly help you identify the error and point it to the client. However, here are your nuances. For example, this expression will not throw an error if the client enters an invalid credit card number. In this case, you should use special algorithms: algorithm Verhuffa, Luhn algorithm, SHA algorithm, and others. Another advantage of regular expressions is that you can combine it with any development language and instantly check for errors. It saves time for both you and your customers. In addition, some payment providers also charge for failed transactions, which can significantly affect the business as a whole.
Many financial institutions widely use the use of card verification algorithms.
How to find a bank card number among a large number of files?
If you slightly modify some of the expressions above, you can determine the bank card numbers in any document.
\b4[0-9]{12}(?:[0-9]{3})?\b.
In the second case, if there are several of these documents, then using \b\d{13,16}\b, you can find expression with the corresponding number of numbers. Of course, the same number of characters shouldn’t be anywhere else.
And finally, \b(?:\D[-]*?){13,16}\b allows you to find the card numbers even if space or dash separates them.