I wan to restrict user to enter quantity which should not more then quantity in stock

I want to restrict user to enter quantity which should not more then quantity in stock,iIn below JavaScript code qty is the quantity which I am fetching from server, and #quantity is the input box where I set default the value to 1. I want that the user can not enter quantity more then qty.

select: function( event, ui ) {
    var names = ui.item.data.split("|");                        
    id_arr = $(this).attr('id');
    id = id_arr.split("_");
    $('#itemNo_'+id[1]).val(names[0]);
    $('#itemName_'+id[1]).val(names[1]);
    $('#packing_'+id[1]).val(names[3]);
    $('#qty_'+id[1]).val(names[4]);
    $('#quantity_'+id[1]).val(1);
    $('#discount_'+id[1]).val(0);   
    $('#price_'+id[1]).val(names[2]);
    $('#total_'+id[1]).val( 1*names[2] );
    calculateTotal();

hare is the HTML :

<td>
    <input disabled type="number" name="data[0][qty]" id="qty_1" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;">
</td>

<td>
    <input type="number" name="data[0][quantity]" id="quantity_1" class="form-control changesNo" autocomplete="off" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;">
</td>

1 answer

  • answered 2018-07-12 07:53 schogges

    You can use the max-attribute but this has no effect until submit.

    If you want to restrict higher inputs you can do smth like this:

    $("#quantity").keyup(function() {
      if($(this).val() > qty) {
        $(this).val(qty);
      }
    });
    

    this will check on every keyup (digit typed in) if value is higher than qty