Regular Expressions 101

Community Patterns

credit card

2

Regular Expression
PCRE (PHP <7.3)

/
^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|6(?:011|5[0-9]{2})[0-9]{12}|(?:2131|1800|35\d{3})\d{11})$
/
gm

Description

Based on http://www.regular-expressions.info/creditcard.html

This example includes test strings.

This only validates the pattern, and not whether the credit card number passes a checksum validation. See the Luhn algorithm for details https://en.wikipedia.org/wiki/Luhn_algorithm

For an implementation in Lasso, see https://github.com/stevepiercy/sp_valid_creditcard

Submitted by Steve Piercy - 9 years ago