nixfuerdiecharts
nixfuerdiecharts

Reputation: 383

Automatically submitting a HTML form after scanning a barcode

I am trying to automatically submit a form after scanning a barcode. The number I scan is always 16 digits long.

<form id="Form" action="action.php" method="post">
  <input id="here" maxlength="16" placeholder="scan..." type="text" tabindex="1" required autofocus>
  <input id="subHere" type="submit">
</form>

<script>
  $('#here').keyup(function(){
    if(this.value.length ==16){
    $('#subHere').click();
    }
  });
</script>​

The restriction of entering 16 digits is working but my form is not automatically submitting after entering the 16 digits.

I also want to clear the form after submitting, so I can scan the next barcode. Do you have any suggestions?

Upvotes: 2

Views: 9464

Answers (2)

nixfuerdiecharts
nixfuerdiecharts

Reputation: 383

Thanks to Fr0zenFyr it is working now:

<form id="Form" action="./js/filehandling.js" method="post">
   <input id="here" maxlength="16" placeholder="scan..." type="text" tabindex="1" required autofocus>
   <input id="subHere" type="submit">
</form>
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>

<script>
  $('#here').keyup(function(){
      if(this.value.length ==16){
      $('#subHere').click();
      }
  });
</script>​

after entering 16 digits, it automatically submits the form

Upvotes: 3

cli-ish
cli-ish

Reputation: 776

Another way would be :

<script>
  $("#input_field").keyup(function() {
    if ($(this).val().length >= 16)
      $('#form').submit();
  })
</script>

Upvotes: 0

Related Questions