form-submitdouble-submit-prevention

how to stop form submition on enter key?


I don't want to submit form.
When I press enter on textbox my form is submited and reloaded automatically.

How to stop submiting form?

My code is.

<form class="cart" id="cart-frm" style="margin-bottom: 10px; ">
    <div class="row">
        <div class="quantity col-md-6 col-sm-6 col-xs-6" style="width:auto;" >
            <label style="float: left; margin-top: 10px" for="quantity">Quantity : </label> 
            <span>&nbsp;</span>
            <input style="float: right;" type="text" class="input-text qty" id='qty' title="Qty" value="1" name="quantity" min="1" step="1">

        </div>
        <div class="quantity col-md-6 col-sm-6 col-xs-6 pull-right">                            
            <button type="button" style="float: right;"   id ='<?php echo $product['id']; ?>' class="btn btn-primary btn-icon add-to-cart-product"> Add to cart</button>
        </div><br>    
    </div>
    <span class="clearfix"></span>
</form>

Solution

  • <script type="text/javascript">
        document.getElementById("cart-frm").onsubmit = function(){
            // if needs to be submited - return true
            // else - return false
            return false;
        };
    </script>