Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop a form from submitting when running barcode scanner

I am trying to stop a form from submitting when using a barcode scanner to input text into the highlighted field. When I press the button on the scanner it automatically tries to submit using a higher precedence submit button in the form. I have tried to use an ignore function in javascript but can't find the key code value for the scanner. Is there a way to set the precedence of the different submit buttons without having to rearrange them? This is how I have the buttons set up in the code:

    <li>
        <cfinput type="submit" name="add" value="Enter Package">
    <cfinput type="submit" name="search" value="Search">                
    <cfinput type="submit" name="reset" value="Reset">
</li>
    </ul>       
</td>
<td>                
        <center><div id="labelImageDiv">
        <img id="labelImage" src="" alt="label preview"/>
    </div>
<ul>
        <li>
           <label for="printersSelect" class="left">Printer:</label>
        <select id="printersSelect" class="right"></select>
        <button id="printButton">Print</button>
    </li>
    <li>
    <div class="packHead">Package Pickup</div>                          
        <label for="pickup" class="left">Scan Barcode:</label>
        <div class="right">
        <cfinput type="text" name="pickup">
    </div>
    </li>
    <li>
    <div id="pickButton">
        <cfinput type="submit" name="pkgPickup" value="PickUp">
    </div>
    </li>
</ul>
like image 304
trueinViso Avatar asked Feb 12 '13 18:02

trueinViso


2 Answers

Best solution I have found so far

$(":input").keypress(function(event){
    if (event.which == '10' || event.which == '13') {
        event.preventDefault();
    }
});
like image 71
Uzair Avatar answered Oct 11 '22 10:10

Uzair


I voted for existdissolve's answer because the scanner should be configurable.

However, a very simple javascript solution would be to return false onSubmit unless the submit button is actually clicked allowing the form to be submitted. Something like this --

<script language="javascript">var p = false;</script>
<form method="post" onsubmit = "return(p)">
    <input type = "text" name = "text" />
    <input type = "submit" value = "submit" name = "submit" onClick = "javascript: p=true;" />
</form>
like image 30
genericHCU Avatar answered Oct 11 '22 12:10

genericHCU