Skip to content

Instantly share code, notes, and snippets.

@hto
Last active April 28, 2019 18:56
Show Gist options
  • Save hto/060a1d3df81929eedd51799b160b79c8 to your computer and use it in GitHub Desktop.
Save hto/060a1d3df81929eedd51799b160b79c8 to your computer and use it in GitHub Desktop.
Regex All Credit Card Validation

Remove all , and - and other non-digits from the string first. Then use this regex that matches Visa, MasterCard, American Express, Diners Club, Discover, and JCB cards:

https://stackoverflow.com/questions/9315647/regex-credit-card-number-tests

Code

^(?:4[0-9]{12}(?:[0-9]{3})?|[25][1-7][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment