title: Credit Card Swiper Javascript Keeps Going
body: <p>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.</p>
  
  <p>Here is the pertinent code:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
          window.onload = function () {
              $("#cn").focus();
              $("#cn").bind("keydown keyup", doItPlease);
          }
          function doItPlease() {
              var count = 0;
              if ($(this).val().indexOf('?') !== -1 &amp;&amp; count &lt; 1) {
                  setFromCCS($(this).val())
                  count++;
              }
          }
     &lt;/script&gt;
  </code></pre>
  
  <p>Here is a  link to the full code: <a href="http://paste2.org/p/1693324" rel="nofollow">http://paste2.org/p/1693324</a></p>
  
  <h2>UPDATE 1:</h2>
  
  <p>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?</p>
  
  <p>Current code: <a href="http://paste2.org/p/1693577" rel="nofollow">http://paste2.org/p/1693577</a></p>
  
  <h2>UPDATE 2:</h2>
  
  <p>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:</p>
  
  <pre><code>$(document).ready(function () {
          var count = 0;
  
          $("#cn").focus();
  
          $("#cn").keyup(function () {
              if ($(this).val().indexOf('?') !== -1 &amp;&amp; count &lt; 1) {
                  var magstripe = $(this).val().substring(0, $(this).val().indexOf('?'));
                  setFromCCS(magstripe);
                  count++;
              }
              else if ($(this).val().indexOf('?') !== -1 &amp;&amp; count &gt; 0) {
                  $("#cn").val($(this).val().substring(0, $(this).val().indexOf(';')));
              }
          });
      });
  </code></pre>
  
  <p>Here is the MagStripe thats being parsed:
  %B9999999999999999^AAAAAAA/AAAAAA^1204101     999?;B9999999999999999=120410110000999?</p>