Kenny
Kenny

Reputation: 1054

IF Statement not working correctly

I have these controls:

    chck1_amt through chck5_amt (1 though 5 check Amounts)

    And chckX is from chcknum1 through chcknum5 (1 through 5 check numbers

What we are trying to do is if a checkX_amt box has is not blank or it has a value other than a 0 AND check number box (chcknumX) is blank, then inform a user that check number box must contain a check number. Stay focused on that box till the user has entered a checknumber.

This appeared to have worked until today when it keeps asking for check number whether there is a value in checkamount box or not.

Here's the code:

if ((document.getElementById('chck1_amt').value != "" || document.getElementById('chck1_amt').value != "0") && (document.getElementById('fvEmp_chcknum1').value == "")) {
    alert("Please enter check # to continue");
    document.getElementById('chcknum1').focus();
    return false;
}
if (((document.getElementById('chck2_amt').value != "") || (document.getElementById('chck2_amt').value != "0")) && (document.getElementById('fvEmp_chcknum2').value == "")) {
    alert("Please enter check # to continue");
    document.getElementById('chcknum2').focus();
    return false;
}
if ((document.getElementById('chck3_amt').value != "0") && (document.getElementById('chcknum3').value == "")) {
    alert("Please enter check # to continue");
    document.getElementById('chcknum3').focus();
    return false;
}
if ((document.getElementById('chck4_amt').value != "0") && (document.getElementById('chcknum4').value == "")) {
    alert("Please enter check # to continue");
    document.getElementById('chcknum4').focus();
    return false;
}
if ((document.getElementById('chck5_amt').value != "0") && (document.getElementById('chcknum5').value == "")) {
    alert("Please enter check # to continue");
    document.getElementById('chcknum5').focus();
    return false;
}
<tr>
    <td><input name="chcknum1" type="text" id="chcknum1" style="width:90px;" /></td><td><input name="chck1_amt" type="text" id="chck1_amt" style="width:90px;" /></td><td>
</tr><tr>
    <td><input name="chcknum2" type="text" id="chcknum2" style="width:90px;" /></td><td><input name="chck2_amt" type="text" id="chck2_amt" style="width:90px;" /></td><td>
</tr><tr>
    <td><input name="chcknum3" type="text" id="chcknum3" style="width:90px;" /></td><td><input name="chck3_amt" type="text" id="chck3_amt" style="width:90px;" /></td><td>
</tr><tr>
    <td><input name="chcknum4" type="text" id="chcknum4" style="width:90px;" /></td><td><input name="chck4_amt" type="text" id="chck4_amt" style="width:90px;" /></td><td>
</tr><tr>
    <td><input name="chcknum5" type="text" id="chcknum5" style="width:90px;" /></td><td><input name="chck5_amt" type="text" id="chck5_amt" style="width:90px;" /></td><td>
</tr>

Upvotes: -1

Views: 142

Answers (2)

James
James

Reputation: 22227

Something like this:

if (document.getElementById('chck1_amt').value && Number(document.getElementById('chck1_amt').value) && !document.getElementById('chcknum1').value && !Number(document.getElementById('chcknum1').value)) {
  alert('Check 1 has an amount but does not have a valid check number');
  document.getElementById('chcknum1').focus();
  return false;
}

etc

Upvotes: 0

James Johnson
James Johnson

Reputation: 46047

I might be missing something, but it looks like your logic is backwards, as anything other than "" or "0" will prompt the user to enter a check number. Shouldn't the condition be something like this?:

EDIT

Thanks for your comment. I understand your question now. I still think that there is an issue with your logic though. If the user should be prompted for input when the value is anything other than "" or 0, the logic should be like this:

if (document.getElementById('chck5_amt').value.trim() != "0" && document.getElementById('chck5_amt').value.trim() != ""){
    //prompt for check number
}

You might want to add some additional checks to make sure that the input boxes are being found:

var input = document.getElementById('chck5_amt');

//uncomment the line below to validate that the input was found
//alert(input);

if (input){
    if (input.value.trim() != "0" && input.value.trim() != ""){
        //prompt for check number
    }
}

Upvotes: 2

Related Questions