How can I create a regex pattern to validate credit card numbers in the following formats ?

Copper Contributor

I am working on a project where I need to validate credit card numbers entered by users. The credit card numbers can be in various formats including:

 

1234-5678-9012-3456
1234 5678 9012 3456
1234567 890123456
1234 **** 90** 3456
1234-*78-901-***6and even with special characters like, “1234$%^&*0123456”.


I would appreciate it if someone could provide me with a regex pattern that can validate these diverse formats.

1 Reply

@Garre_Akhil
Validating credit card numbers with various formats can be achieved using a regular expression. Here's a regex pattern that should work for the formats you've provided: "^(?:\d[ -]*?){13,16}$". This pattern allows for digits, spaces, and dashes between the digits. It requires at least 13 digits and accepts up to 16 digits. You can use this pattern to validate credit card numbers in the formats you've mentioned.