0

I have a form with 2 Submit buttons. One is for Approve the other is for Reject.

The HTML is:

<form action="test.php" method="post" name="sos_submit" id="sos_submit">

  <input type="text" name="qty_indented" value="<?php echo $qty_indented ?>" id="qty_indented" readonly="readonly" />
  <input type="text" name="qty_approved" value="<?php echo $qty_approved ?>" id="qty_approved" />
  <input type="submit" name="da_approve" value="APPROVE" onclick="return submitForm(this)" />
  <input type="submit" name="da_reject" value="REJECT" onclick="return submitForm(this)" />

</form>

And my submitForm(this) Function looks like:

<script>
function submitForm (button){

    if (button.value == "APPROVE"){
        var qty_approved = document.getElementById("qty_approved").value;
        var qty_intended = document.getElementById("qty_indented").value;
        if (qty_approved <= 0){
            alert("Please Enter the Quantity for Approval!");
            submitOK = "false"; 
        }
        else if(qty_approved >= qty_intended){
            alert("Quantity Approved " + qty_approved + " Cannot be more than quantity intended " + qty_intended + " !");
            submitOK = "false";
        }
        if (submitOK == "false") {
            return false;
        }
    }
    else{
        confirm("Are you sure you want to REJECT the Voucher?");    
    }
}
</script>

Although its very simple code, but somehow not working properly.

Even though qty_approved is LESS than qty_indented, the if() statements are executing.

For example, if qty_approved = 5 and qty_indented = 10, then the code should submit the form, but it is NOT. It is showing: Quantity Approved 5 Cannot be more than quantity intended 10 !

Did I do anything wrong?

2
  • 1
    .value returns a string. qty_approved >= qty_intended performs string comparison. Commented Jan 15, 2015 at 16:36
  • 1
    They are getting compared as strings -- try to make them int. Commented Jan 15, 2015 at 16:37

1 Answer 1

4

Input boxes are string-based values. You need to convert the values to integers using parseInt:

var qty_approved = parseInt(document.getElementById("qty_approved").value, 10);
var qty_intended = parseInt(document.getElementById("qty_indented").value, 10);

At the moment it's comparing '5' to '10', and alphabetically '5' is greater than '10', which is why it's dropping into that condition.

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

1 Comment

That actually solved the issue! I will vote up when it allows me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.