Ryan H
Ryan H

Reputation: 153

How to use RegEx to mask valid credit cards AND masked cards

I have regex masks in place to validate the cards I want but we are starting to using payment profiles with our credit card vendor and I want to allow partially masked cards in the fields as well but can't figure it out.

So for example; I want to allow;

4111123412341234

and

XXXXXXXXXXXX1234

The last 4 digits will be the last four digits of the credit card which comes from our credit card processor. For the above example, my card mask 1 is ^4\d{0,15} and I need to figure out how to modify to allow the XXXXXXXXXXXX1234 as well;

Upvotes: 0

Views: 331

Answers (1)

vmp
vmp

Reputation: 2420

If what you want is either 16 digits or 12 X's followed by 4 digits then what you want is:

^(\d{16}|X{12}\d{4})$

Upvotes: 1

Related Questions