How to establish a minimum and maximum amount of a value required by an input in a JS request?

Asked

Viewed 83 times

4

I have a field called Quantidade that the quantity of products for sale should be inserted. My form has a data request that returns the value and quantity of products in stock, this field Quantidade returns that amount of stock of products because of the id="quantidade", then you already receive a value.

Only that the field Quantidade I wish I had a minimum and maximum limit for the inserted quantity using min and max within that <input>, placing a PHP code inside the max to set the maximum amount entered (the min can be zero).

Example: If the field Quantidade receives a value of 30 products because of data request, this field must allow an insertion of at least 0 up to the maximum 30, leaving the field more intuitive.

JS code that requests the value in the field Quantidade

var retornoJson = JSON.parse(ajax.responseText);
document.querySelector("#quantidade").value = retornoJson[0].quantidade;

HTML code:

<p> Quantidade: <input type="number" name="quantidade" id="quantidade" required=""> </p>
  • 1

    There is, in fact, the type type="int" of <input> in HTML?

  • Yes, that piece of code <p> Quantidade </p> that I posted, wanted it to be more intuitive in the amount it gets to set the maximum amount entered allowed.

  • type="int"´´´ o <input>já possui, mas o usuário ele não sabe o valor mínimo é máximo para digitar nesse<input>```, only returns the stock amount of a data request (eg: 30) wanted to prevent values above the allowed.

  • agree with o@Woss, do not know type=int, would be a type=number ai certo? https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input

  • 1

    https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attributes The point is that int is not a valid type for the <input>; for numerical values should be used number.

  • There is no such type="int" no, we have 21 types and none of them is int. It has the type number, but who accepts max and min is the type range

Show 1 more comment

1 answer

6

You don’t need PHP for this. If you already make the request to get the value, just define it with Javascript even:

const quantidade = document.getElementById("quantidade");

quantidade.min = 0
quantidade.max = 5;
input:invalid {
  border: 2px solid red;
}
Quantidade: <input type="number" id="quantidade">

Note that even if the user can enter a value greater than the maximum set, the field becomes invalid according to the Javascript validation API, which can be proven by the added CSS, which turns the field red.

Another legal field that can be used is the type range:

Quantidade: <input type="range" id="quantidade" min=0 max=5>

  • "min" and "max" tbm can be added to the input type=number tag without needing javascript: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/number

  • @Ricardopunctual Yes, but he gets the value of max via request to an API in JS; not fixed.

  • @Woss how to prevent manual change of the first code that uses JS? My field Quantidade has a JS mask that prevents quantity above 4 digit number.

  • What would be "manual change"?

  • i click on the field and enter a value without using the field arrows?

  • Is there any way I can change the quantidade.max = 5; for something that returns the value of ```id="quantity"````?

  • And why would it prevent the user from doing this? If he wanted the number 10, would he have to click 10x? It seems to me unfeasible.

Show 2 more comments

Browser other questions tagged

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