Shermaine Chaingan Shermaine Chaingan - 4 months ago 20
Ajax Question

Validate two textbox Greater than

how i will automatic validate when i put value on quantitytotransfer using ajax or javascript the two textbox it should not greater than the
quantity on hand the value of quantitytotransfer ?

<input type="number"
class="form-control"
readonly
name="quantityonhand"
id="quantityonhand">

<input type="number"
required
class="form-control"
name="quantitytotransfer"
id="quantitytotransfer"
maxlength="11"
onkeypress="return isNumber(event);"
required>

function verify() {
var n = $("#quanitytotransfer").val();
if (n => $( "#quanityonhand" ).val())
{
alert("greater than quantity on hand");
}
else
{
alert("greater than quantity on hand");
}

</script>

Answer

you need to use parseInt to check if one value is greater than the other. also, you need to bind this function to the input

<script>
$(document).ready(function() {
  $("#quantitytotransfer").on("input", function() {
    verify()
  });
});

function verify() {
  var firstValue = parseInt($("#quantitytotransfer").val());
  var secondValue = parseInt($("#quantityonhand").val());
  if (firstValue >= secondValue) {
    alert("greater or equal than quantity on hand");
  } else {
    alert("smaller than quantity on hand");
  }
}  
</script>

https://jsfiddle.net/shirandror/5w9a0o6e/