TESTEVERYTHING

Thursday 4 March 2021

How to validate credit cards using a regular expression?

 Again, you should rely on other methods since the regular expressions here will only validate the format. Make use of the Luhn algorithm to properly validate a card.

VISA:
^4[0-9]{12}(?:[0-9]{3})?$
MasterCard:
^5[1-5][0-9]{14}$
American Express:
^3[47][0-9]{13}$
Diners Club:
^3(?:0[0-5]|[68][0-9])[0-9]{11}$
Discover:
^6(?:011|5[0-9]{2})[0-9]{12}$
JCB:
^(?:2131|1800|35\d{3})\d{11}$

No comments:

Post a Comment

Which one is right ?

Translate







Tweet