Regex for Credit Card Number Validation
Quick Answer: Visa: ^4\d{12}(?:\d{3})?$, Mastercard: ^5[1-5]\d{14}$, Amex: ^3[47]\d{13}$. Regex validates the number format and issuer prefix only. Always apply the Luhn algorithm for checksum validation, and use your payment processor for actual card verification.
FAQ
How do I validate a credit card number?
Three steps: (1) regex to check format and detect issuer, (2) Luhn algorithm for checksum validation, (3) payment processor API for actual card verification.
What is the Luhn algorithm?
Luhn is a checksum formula that catches single-digit errors and most transpositions. The last digit of a card number is a check digit computed from the other digits.