Search code examples
javascriptformsvalidationbootbox

Javascript not executing if statements sequentially


I'm trying to write a form validator. I expect that it will check all the if statements and finally give a true or false result. But it is returning always true as soon as it called.

function validator() {
    var ret = true;

    fname = document.getElementById('fname').value;
    newp = document.getElementById('newp').value;

    if (fname == null || fname == "") {
        bootbox.alert("Name is empty");
        ret = ret && false;

    }

    if (newp.length > 0) {

        bootbox.confirm("Account will be locked",
            function(result) {
                if (result == true) {
                    ret = ret && true;

                } else {
                    ret = ret && false;

                }
            });
    }

    return ret;

}

Solution

  • The bootbox confirmation dialog will not block execution of that outer "validator" function. That outer function will return while the confirmation dialog is still showing.

    Using something like the Bootstrap dialog code, you really cannot make a function like that. The callback function you pass to the confirmation dialog will be invoked, but not until the user interacts with the dialog.