Credit Card Matching
The following patterns are useful for first stage of credit card validation for Visa and Mastercards.
Visa pattern:
/^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/
Mastercard pattern:
/^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/
This does not prove the number is valid but that it contains the correct content so it is a handy and light weight first stage validation.
To fully validate you must calculate a check digit. If this check digit is divisible by 10 then you have validated the Visa or Mastercard number!