-2

I am looking to write a Regex in javascript which match only master/visa card with spaces as below:

5555 5555 5555 4444 (Valid input master card)
4111 1111 1111 1111  (Valid input visa card) 
3782 8224 6310 005   (Invalid input AMEX)

I have wrote a custom validator in javascript which validates against the following Regex:

regexp: {
              regexp: /^(?:4[0-9]d{12}(?:[0-9]d{3})?|(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{d12})$/,
              message: "Only visa and master cards are allowed"
            }

The event is fires which uses the above regex and displays the message based on user input. Few examples of user input below:

Credit Card     Sample Number                Output
Visa            4111 1111 1111 1111          Match
MasterCard       5500 0000 0000 0004         Match
American Express    3400 0000 0000 009       Invalid
Diner's Club      3000 0000 0000 04          Invalid 
Carte Blanche    3000 0000 0000 04           Invalid
Discover          6011 0000 0000 0004        Invalid
en Route          2014 0000 0000 009         Invalid
JCB               3088 0000 0000 0009        Invalid 
Arun Kumar
  • 907
  • 13
  • 33

1 Answers1

0

Here you go...

^(?:4[0-9]{12}(?:[0-9]{3})?          # Visa
 |  (?:5[1-5][0-9]{2}                # MasterCard
     | 222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}
 |  3[47][0-9]{13}                   # American Express
 |  3(?:0[0-5]|[68][0-9])[0-9]{11}   # Diners Club
 |  6(?:011|5[0-9]{2})[0-9]{12}      # Discover
 |  (?:2131|1800|35\d{3})\d{11}      # JCB
)$

source: https://www.regular-expressions.info/creditcard.html