I've got a script that binds to the keyup event as a page is loaded, this sets the cursor into the first field and then when the credit card is swiped it chops up the magnetic stripes data, but currently the code seems to be running multiple times and I'm not sure how to just have it run once.
Here is the pertinent code:
<script type="text/javascript">
window.onload = function () {
$("#cn").focus();
$("#cn").bind("keydown keyup", doItPlease);
}
function doItPlease() {
var count = 0;
if ($(this).val().indexOf('?') !== -1 && count < 1) {
setFromCCS($(this).val())
count++;
}
}
</script>
Here is a link to the full code: http://paste2.org/p/1693324
UPDATE 1:
Adding $(document).ready(function()) solved a good chunk of the issue but I'm still not getting the result I need. It still is running twice since there are two question marks (?) in the string that is being input. Is there any easy way to limit this to run only once?
Current code: http://paste2.org/p/1693577
UPDATE 2:
Okay, so I've got a working solution, one where the script will re-parse the string and replace it. I feel like this is a hack and there is a better way to do it, just not sure how at the moment. Here is the code:
$(document).ready(function () {
var count = 0;
$("#cn").focus();
$("#cn").keyup(function () {
if ($(this).val().indexOf('?') !== -1 && count < 1) {
var magstripe = $(this).val().substring(0, $(this).val().indexOf('?'));
setFromCCS(magstripe);
count++;
}
else if ($(this).val().indexOf('?') !== -1 && count > 0) {
$("#cn").val($(this).val().substring(0, $(this).val().indexOf(';')));
}
});
});
Here is the MagStripe thats being parsed: %B9999999999999999^AAAAAAA/AAAAAA^1204101 999?;B9999999999999999=120410110000999?