Adding an if statement inside if statement

16,122

Solution 1

How about:

if (!($finishBoxes.length == $finishBoxes.filter(':checked').length && 
    $('#tcaccept').val() == '1' && 
    !($("#ctl").val() == "1" && $('#ctlapp').val() === "")))

What we're adding here is another condition which says, "And make sure it's not the case that #ctl is 1, and #ctlapp is blank".

Edit: and please see my comment above - your question is not about jQuery, forms, or validation. It's barely about JS.

Solution 2

if($('#tcaccept').val() == '1' && validate()) {
 // do something
}

var validate = function(){
  if ($("#ctl").val() == "1") {
    $('#ctlapp') must not be blank
    return false;
  }
  else if ($("#ctl").val() == "0") {
    $('#ctlapp') can be blank;
    return true;
  }
  return true;
}

Solution 3

I'd say clean up the code a little, and things will get a bit simpler:

$('#submit2pd').click(function(event) {
    var fail_message = 'Please complete the Induction Checklist confirming that you have read and understood the Colleges main policies and procedures, agreeing to comply with these in line with the terms of your contract with the College',
        $finishBoxes = $('#co1,#co2,#co3,#div1,#cc1'),
        $checkedBoxes = $finishBoxes.filter(':checked'),
        tcaccept = $('#tcaccept').val(),
        ctl = $("#ctl").val(),
        ctlapp = $('#ctlapp').val();

    if (tcaccept == '1' && $finishBoxes.length != $checkedBoxes.length ) {
        alert( fail_message );
        return false;
    } else if( ctl == "1" && ctlapp == "" ) {
        alert( "some other message" );
        return false;
    }

    // otherwise the form is submitted
    window.location.href = "submit2pd.php";
});

I left the test for $('#ctl').val() == "0" out because it sounds like that's the only other value it can have, and there's no validation that needs to take place if it is "0".

Share:
16,122
Codded
Author by

Codded

Updated on June 08, 2022

Comments

  • Codded
    Codded almost 2 years

    I have a function for a button which submits a form. This function checks to see if the 5 checkboxes are selected #co1,#co2,#co3,#div1,#cc1.

    It then also checks to see if tcaccept select is set to 1. If so the form submits, else the alert pops up.

    This is the code i have at the moment:

      $('#submit2pd').click(function(event) {
        var $finishBoxes = $('#co1,#co2,#co3,#div1,#cc1');
    
            if (!($finishBoxes.length == $finishBoxes.filter(':checked').length && $('#tcaccept').val() == '1' )) {
        alert('Please complete the Induction Checklist confirming that you have read and understood the Colleges main policies and procedures, agreeing to comply with these in line with the terms of your contract with the College');
                return false;
            }
    
            // otherwise the form is submitted
            window.location.href = "submit2pd.php";
    
        });
    

    All works brilliantly, but i want to add to this line as i have another option that is required. But this needs to be an if statement.

        if (!($finishBoxes.length == $finishBoxes.filter(':checked').length && $('#tcaccept').val() == '1' && THE IF STATEMENT))
    

    this is what i need to incorporate into the if statement above.

    if ($("#ctl").val() == "1") {
    $('#ctlapp') must not be blank
    }
    else if ($("#ctl").val() == "0") {
    $('#ctlapp') can be blank
    }
    

    Any help would be greatly appreciated.

  • aziz punjani
    aziz punjani over 12 years
    Wow, i'd hate to be the one debugging this statement if there was a bug.
  • zuloo
    zuloo over 12 years
    what if $("#ctl").val() == "1000"
  • aziz punjani
    aziz punjani over 12 years
    Once you work with several other developers in a team, you WILL learn to care. This is not maintainable code, hard to read and just makes me want to cry.
  • N3dst4
    N3dst4 over 12 years
    @zuloo although I strongly suspect that in practice, the only possible values for $("#ctl").val() will turn out to be "0" and "1".
  • zuloo
    zuloo over 12 years
    i have many coworkers and they like the way I indent complex if-statements. sure you could break this in pieces and make the code more complex (like defining a validation function that ends up 100+ lines away from the statement...), but i cant see where your problem is, it is absolutely clear where a partial condition starts and where it ends.
  • N3dst4
    N3dst4 over 12 years
    @MatthewBrennand Ignore my snarkiness, I'm just in a snarky mood. But if you can understand why your question is not about jQuery, you will have taken an important step on the path to enlightenment.
  • Codded
    Codded over 12 years
    @N3dst4: This works like a charm. Would never get anywhere and learn if i did not ask questions. Thanks for the advice.
  • Codded
    Codded over 12 years
    Thanks for the advice, i have it working now but when i get time i will follow the advice and clean it up a bit. I like the seperate alerts.