Ali Habibzadeh
Ali Habibzadeh

Reputation: 11568

JavaScript statement execution order

In the following code how can I still have the second and third statement executed even if the first one returns false?

// so in this one if Required fails I dont get the email error :(
function validate() {
    if (checkRequired(myForm.requiredElements)
        && checkEmail(myForm.emailInputs)
        && checkTelephone(myForm.telInputs)) {
        return true;
    }
    else {
        return false;
    }
}

Upvotes: 0

Views: 404

Answers (1)

joshuahealy
joshuahealy

Reputation: 3569

Use the single-ampersand version of and, it doesn't shortcircuit like the double-ampersand one.

function validate() {
    if (checkRequired(myForm.requiredElements)
        & checkEmail(myForm.emailInputs)
        & checkTelephone(myForm.telInputs)) {
        return true;
    }
    else {
        return false;
    }
}

Upvotes: 5

Related Questions