Adding an if statement inside if statement

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.

Answers


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.


Need Your Help

Java Web Start and Security

java swing java-web-start

It was just pointed out to me that the Java Web Start applications from my blog, for example my Key Bindings entry, no longer function because of the follow security error:

Why is there a difference in giving values to global javascript objects locally?

javascript global-variables javascript-objects local-variables

Please explain why changing a global objects string property in local scope does affect the property in a global scope but giving a new value to the whole object doesn't.