验证两个文本框大于

时间:2016-07-08 01:38:11

标签: javascript ajax

当我使用ajax或javascript将值转换为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>

1 个答案:

答案 0 :(得分:0)

您需要使用parseInt检查一个值是否大于另一个值。此外,您需要将此函数绑定到输入

<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/