Regular expression: validation of credit card numbers

In the Mendix Business Modeler you can add regular expressions. I would like verify whether the customer entered a valid credit card number on an order form. Can anyone help me with getting a good regular expression for it. Perhaps it is also nice when I am able to determine the type of credit card being used. I've heard that each card issuer has its own range of card numbers (identified by the first 4 digits).
4 answers

The short solution, click.

The long solution:
Validating credit card numbers is the ideal job for regular expressions. They're just a sequence of 13 to 16 digits, with a few specific digits at the start that identify the card issuer. You can use the specific regular expressions below to alert customers when they try to use a kind of card you don't accept, or to route orders using different cards to different processors. All these regexes were taken from RegexBuddy's library.

  • Visa: ^4[0-9]{12}(?:[0-9]{3})?$ - All Visa card numbers start with a 4. New cards have 16 digits. Old cards have 13.
  • MasterCard: ^5[1-5][0-9]{14}$ - All MasterCard numbers start with the numbers 51 through 55. All have 16 digits.
  • American Express: ^3[47][0-9]{13}$ - American Express card numbers start with 34 or 37 and have 15 digits.
  • Diners Club: ^3(?:0[0-5]|[68][0-9])[0-9]{11}$ - Diners Club card numbers begin with 300 through 305, 36 or 38. All have 14 digits. There are Diners Club cards that begin with 5 and have 16 digits. These are a joint venture between Diners Club and MasterCard, and should be processed like a MasterCard.
  • Discover: ^6(?:011|5[0-9]{2})[0-9]{12}$ - Discover card numbers begin with 6011 or 65. All have 16 digits.
  • JCB: ^(?:2131|1800|35\d{3})\d{11}$ - JCB cards beginning with 2131 or 1800 have 15 digits. JCB cards beginning with 35 have 16 digits.

If you just want to check whether the card number looks valid, without determining the brand, you can combine the above six regexes into

You'll see I've simply alternated all the regexes, and used a non-capturing group to put the anchors outside the alternation. You can easily delete the card types you don't accept from the list.

These regular expressions will easily catch numbers that are invalid because the customer entered too many or too few digits. They won't catch numbers with incorrect digits. For that, you need to follow the Luhn algorithm, which cannot be done with a regex. And of course, even if the number is mathematically valid, that doesn't mean a card with this number was issued or if there's money in the account. The benefit or the regular expression is that you can put it in a bit of JavaScript to instantly check for obvious errors, instead of making the customer wait 30 seconds for your credit card processor to fail the order. And if your card processor charges for failed transactions, you'll really want to implement both the regex and the Luhn validation.

Source: here


You don't have to use some regular expression to validate the credit card numbers. I doubt if this would even give the correct result. Because there are some rules that have to be applied to the number.

If you would have searched with google you would have seen that credit card numbers can be validated with the Luhn algorithm. The luhn algorithm can be implemented in java see the last link. And in microflow you could check which prefix is used and which card issuer has that prefix.

Wiki explanation:
Explanation of the algorithm and cardtype prefix:
Java example:


You can find a library with a lot of regular expression at this website.

Ps. use google you can find a lot of reg ex on internet.


Your soloution (click)