Search code examples
regexregex-greedy

Regex credit card number tests


I'm testing one application where Regex pattern match credit card then such numbers should be highlighted. I'm using site http://regexpal.com/ to create test credit credit card numbers for my testing. my requirement is to have valid credit card numbers which can have "-" and/or "," between them.I was not successful to build such a number as when i test it using the site

http://regexpal.com.

I need few credit numbers with scenarios below

  1. valid credit card number which can have "-" between any digit.
  2. valid credit card number which can have "," between any digit.
  3. valid credit card number which can have cobination of "," or "-" between any digit.

Solution

  • 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:

    ^(?: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})$