0

Below is my function for checking and preventing to input a value greater than the available quantity. It is triggered by onBlur.

Suppose: intData = 7 intOnHand = 46

When it reaches if(intData > intOnHand) it enters the code. Which supposedly it should not. Because 7 > 46 is false. Weirdly enough this happen only when input data is between 5-9. And greater than 46(which is correct).

Output of my alert:
intData7
inOnHand46
Qty to Borrow Must not greater than the qty on the inventory!

function CheckInput(intData){

      var mode = $('#mode').val();

      intOnHand = $('#qtyin').html();
      if(mode == 'Borrow'){
           if(intData > intOnHand){
               alert("intData"+intData);
               alert("inOnHand"+intOnHand);
                alert("Qty to Borrow Must not greater than the qty on the inventory!");
                $("#QtyToReturn").val(intOnHand);

           }
      }

 }

1 Answer 1

3

The problem is you are doing a string comparison because intOnHand is of type string, convert that to a numerical type before the comparison

function CheckInput(intData) {

    var mode = $('#mode').val();

    var intOnHand = +$('#qtyin').html();
    if (mode == 'Borrow') {
        if (+intData > intOnHand) {
            alert("intData" + intData);
            alert("inOnHand" + intOnHand);
            alert("Qty to Borrow Must not greater than the qty on the inventory!");
            $("#QtyToReturn").val(intOnHand);

        }
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh I see. I'll try casting it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.