Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

(Javascript) oninput with numbercheck

I’m trying to make a simple inventory systems. But I’m having problem with my oninput event.

I want to make TOTAL GOODS to be "Please input number in GOODS IN " whenever every non number value inserted into GOODS IN. But it seems I can’t make it so.

/*MAKE EVERY TABLE CLICKABLE AND SHOW ROW DATA IN INPUT TEXT*/

var tbGoods = document.getElementById('tbGoods');
for (var i = 0; i < tbGoods.rows.length; i++) {
  tbGoods.rows[i].onclick = function() {
    document.getElementById("idTxt").value = this.cells[1].innerHTML;
    document.getElementById("gdTxt").value = this.cells[2].innerHTML;
    document.getElementById("qtyTXT").value = this.cells[3].innerHTML;

    var qty = parseInt(document.getElementById('qtyTXT').value);
    var x = parseInt(document.getElementById('gdin').value);
    var result = qty - x;
    document.getElementById('totalgd').value = result;


  };
}

/*MAKE EVERY NUMBER I PUT IN GOODS IN, TO BE CALCULATED WITHOUT SUBMIT BUTTON (ONINPUT)*/

function testmin() {
  var qty = parseInt(document.getElementById('qtyTXT').value);
  var x = parseInt(document.getElementById('gdin').value);
  var result = qty - x;
  if (document.getElementById('gdin').value === '') {
    document.getElementById('totalgd').value = '0';
  } else if (document.getElementById('qtyTXT').value === '') {
    document.getElementById('totalgd').value = '0';
  } else if (Number.isNaN(document.getElementById('gdin').value)) {
    document.getElementById('totalgd').value = 'Please Input Number in Goods In';
  } else {
    document.getElementById('totalgd').value = result;
  }
}
<form method="post">
  <label>ID</label>
  <input type="text" name="id" id="idTxt" disabled>

  <label>GOODS</label>
  <input type="text" name="goods" id="gdTxt" disabled>

  <label>AVAILABLE QTY</label>
  <input type="text" name="qty" id="qtyTXT" disabled>

  <label>GOODS IN</label>
  <input type="text" name="gdin" id="gdin" oninput="testmin()">
  <br>
  <br>

  <label>Total Goods</label>
  <input type="text" name="totalgd" id="totalgd" value="0" disabled>
  <br>

  <input type="submit" name="submit" value="submit">
</form>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You don’t need to code that manually. You can simply set the input type as "number" and your browser will not allow any non-numeric characters to be entered into the field.

Demo (run the snippet and try typing in the box):

<input type="number" id="gdin" name="gdin"/>

Reference: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/number

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading