Remove negative signal from input number

Asked

Viewed 3,624 times

-1

I have the following code in jquery:

$.each(data.Dados, function (i, val) {
   tr += '<tr>';
   tr += '<td><div class="product" style="margin-bottom:0;padding-bottom:0;"><div class="product-info" style="margin-bottom:0;padding-bottom:0;"><h3 class="product-title">' + data.Dados[i].post_title + '</h3></div></div></td>';
   tr += '<td style="text-align:center;" class="price-col" data-id-cart="' + data.Dados[i].post_id + '" id="preco_' + data.Dados[i].post_id + '">' + data.Dados[i].post_valor + '</td>';
   tr += '<td style="text-align:center;padding:0;" class="quantity-col"><input data-mask="00" data-mask-selectonfocus="true" style="margin:0;" onfocus="campoTexto(this, 1)" onblur="campoTexto(this, 0)" type="number" onChange="Multiplica(' + data.Dados[i].post_id + ',' + data.Dados[i].post_quantidade + ' )" class="form-control qtd_' + data.Dados[i].post_id + '" ' + data.Dados[i].disabled + ' min="1" max="' + data.Dados[i].post_quantidade + '" placeholder="0" required=""></td>';
   tr += '<td style="text-align:center;" class="subtotal-col">R$<span id="sub_' + data.Dados[i].post_id + '">0.00</span></td>';
   tr += '</tr>';
});

I need to clear the -(less) signal from this blessed input field, because if a customer selects -1 the values in real time will be negative, so I can’t let that -1 be typed, if selecting via arrows from the field it blocks, the more the problem is when type.

I think I might have a regular expression that I was able to remove even by typing, or else not allow the sign in the field.

  • property min="0" solves...

  • things like this find easily in other topics, google and related....

  • this property min="0" when you type the minus sign appears in the input

2 answers

4


Some solutions:

<input min='0' type="number" onkeypress="return (event.charCode == 8 || event.charCode == 0) ? null : event.charCode >= 48 && event.charCode <= 57">

or

<input type="number" onkeypress="return event.charCode >= 48" min="1" >

or even this

<input type="number" min="0" oninput="this.value = Math.abs(this.value)">

or who knows this

<input type="number" name="test" min=0 oninput="validity.valid||(value='');">

  • Solved with the second option

0

uses property min="0"

<input data-mask="00" data-mask-selectonfocus="true" style="margin:0;" onfocus="campoTexto(this, 1)" onblur="campoTexto(this, 0)" type="number" onChange="Multiplica(' + data.Dados[i].post_id + ',' + data.Dados[i].post_quantidade + ' )" class="form-control qtd_' + data.Dados[i].post_id + '" ' + data.Dados[i].disabled + ' min="1" max="' + data.Dados[i].post_quantidade + '" placeholder="0" required="" min="0">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.