Home > Back-end >  How to make only one integer can be written in the box?
How to make only one integer can be written in the box?

Time:05-11

I want to make only 1 number can be written in the box (maximum is number 8). Is there anyway to do that?

<label  for="seal_qty">Seal Quantity:</label>
      <div >
        <p  style="margin-top: -6px;">
            <input type="number"  id="seal_qty" name="seal_qty" min="1" max="8" placeholder="Enter Seal Quantity" value="<?php echo $seal_qty; ?>">
        </p>
      </div>
      <div ></div>

var seal_qty = document.translot.seal_qty.value;
    if (seal_qty == null || seal_qty ==""){
        alert("Seal Quantity should not be blank.");
        return false;
    } 

CodePudding user response:

You can check lenght and number like:

const seal_qty = document.querySelector('#seal_qty');
seal_qty.addEventListener('change', (e) => {
  const input = e.target;
  const value = parseInt(input.value);
  if(value.length > 1 || value > 8){
    input.value = '';
    alert("maximum is 8.");
  }
});
<label  for="seal_qty">Seal Quantity:</label>
<div >
  <p  style="margin-top: -6px;">    
    <input type="number"  id="seal_qty" name="seal_qty" min="1" max="8" placeholder="Enter Seal Quantity">
    
  </p>
</div>
<div ></div>

  • Related