mardi 11 juin 2019

JavaScript - functions not working as expected

I have these two functions to test if, on my form, a section of 4 rows of elements, each with 4 text boxes and two radio buttons, is "complete."

Complete being 1-3 rows are fully filled, and the rest are empty, or all 4 rows are fully filled and none are empty.

If row 1 is not fully filled and the rest are empty, it is not complete.

If row 1 is fully filled and row 2 is not, it is not complete, and so on.

Right now these functions are saying the form is complete when at least 1 row is fully filled, regardless if the rest are not fully filled.

Are there any errors in this code? I've tried many different fixes and none seem to work - I'm wondering if my logic is off.

function lineIsComplete(i) {
    let lastName = document.getElementById("txtChildsPlacementChild" + i + "LastName");
    let firstName  = document.getElementById("txtChildsPlacementChild" + i + "FirstName");
    let caseNumber = document.getElementById("txtChildsPlacementChild" + i + "CaseNumber");
    let birthDate  = document.getElementById("txtChildsPlacementChild" + i + "BirthDate");
    let apprehended = document.getElementById("rdoChildsPlacementChild" + i + "StatusApprehended");
    let remainHome  = document.getElementById("rdoChildsPlacementChild" + i + "StatusWillRemainInTheHome");

    if (lastName != '' && firstName != '' && caseNumber != '' && birthDate != '' &&
       (apprehended.checked || remainHome.checked)) {
        return true;
    } else if (i != 1 && lastName == '' && firstName == '' && caseNumber == '' && birthDate == '' &&
        (!apprehended.checked || !remainHome.checked)) {
        return true;
    }

    return false;
}

function section4Complete() {
    for (let i=1; i++; i<=4) {
        if (!lineIsComplete(i)) {
            return false;
        } else {
            continue;
        }
    }

    return true;
}

Thanks.

Aucun commentaire:

Enregistrer un commentaire