1

I have a card swipe and I am creating a on site Kioske and need to prefill a credit card form after card swipe. I am not sure why It is not recognizing any cards. Here is an example of a swipe:

%B5500005555555559"TORRANCEJACK G P        "2009206000000000000326000000

Can someone explain why it is not passing the following regex pattern exec?

// MasterCard starts with 51-55, and is 16 digits long.
var pattern = new RegExp("^%B(5[1-5][0-9]{14})\\^([A-Z ]+)/([A-Z ]+)\\^([0-9]{2})([0-9]{2})");
var match = pattern.exec(rawData);

Thank you!

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • I'm not sure, but this two tutorial help you a lot - [Regex](https://tutorialzine.com/2014/12/learn-regular-expressions-in-20-minutes) -[Credit Card](https://tutorialzine.com/2016/11/simple-credit-card-validation-form) – Momin Oct 10 '17 at 04:22
  • One way to debug this would be to start with a much smaller regex (the beginning) and work your way toward the end and see when it breaks. – aaaaaa Oct 10 '17 at 04:31
  • 1
    closer: `var pattern = /%B(5[1-5][0-9]{14})\"([A-Z ]+)([A-Z ]+)\"([0-9]{2})([0-9]{2})/` aside: use a literal, it's much easier to read than an escaped regexp string – dandavis Oct 10 '17 at 05:32
  • @dandavis It is now validating and I agree it is better to read using a literal. I think I can work from your example. Thank you very much. – Jack Torrance Oct 10 '17 at 05:49
  • @dandavis why was it not validating with the provided regex from my example? I am using https://github.com/CarlRaymond/jquery.cardswipe btw – Jack Torrance Oct 10 '17 at 06:06
  • you had quote as caret and an extra slash – dandavis Oct 10 '17 at 06:11
  • If you want a substantial answer, please explain what the expected result in `match` you expect. Certainly you *can work from the example* above, but what good is a question that is not clear? It will be closed and eventually deleted. – Wiktor Stribiżew Oct 10 '17 at 06:42
  • Please check https://regex101.com/r/lQNKoq/1 and let know if you need this. – Wiktor Stribiżew Oct 10 '17 at 07:18

2 Answers2

1

I suggest that you use the following pattern:

/%B(5[1-5][0-9]{14})"([A-Z ]+?)\s+"([0-9]{2})([0-9]{2})/

See the regex demo. Note you will have to fine tune the last ([0-9]{2})([0-9]{2}) groups to get the right digits into the necessary number of groups.

JS demo:

var rx = /%B(5[1-5][0-9]{14})"([A-Z ]+?)\s+"([0-9]{2})([0-9]{2})/g;
var s = '%B5500005555555559"TORRANCEJACK G P        "2009201800000000000326000000';
var matches = rx.exec(s);
if (matches) {
 console.log("Number: " + matches[1]); // => number
 console.log("Name: " + matches[2]); // => name
 console.log("Exp. year: " + matches[3]); // => exp year
 console.log("Exp. month: " + matches[4]); // => exp month
}
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • This works too, same as @dandavis - The last set of digits however are just to be used the first 4 digits which represent year (20) and month (09). The rest of the digits are to be discarded. It works great regardless, thanks! – Jack Torrance Oct 11 '17 at 03:48
0

Found it from here Here

^(?:5[1-5][0-9]\d{1}|222[1-9]|2[3-6][0-9]\d{1}|27[01][0-9]|2‌​720)([\ \-]?)\d{4}\1\d{4}\1\d{4}$

not sure about your regex working but says error to me on that slash /

Test this regex in Regex Tester this is working

Also i found useful something from here

If you need real program to prove working i make it for you if you need in snippet

  • This is not what I am trying to achieve. I have a hardware that reads from magnetic card as shown in the example and It brings the card number, cardholder name as well as the expiration date. I need to validate the regex provided. It needs to break the provided data in 3, card number, carholder name and expiration date. – Jack Torrance Oct 10 '17 at 04:54