jeudi 23 avril 2020

Nested Ifs in JS returning undefined

My function should do some form checks befor submitting. I wrote this routine for checking:

function form_saveable(){
    if (!$('#id_emplacn').val()){
        return false;
    } else if (!$('#id_emplfirst').val()){
        status_code_handler(4001);
        return false;
    } else if (!$('#id_empllast').val()){
        status_code_handler(4002);
        return false;
    } else if ($('#id_emplbd').val()){
//Function gets stucked here
        let input = new Date($('#id_emplbd').val());
        let today = Date.now();
        if (input >= today){
            status_code_handler(4003);
            return false;
        }
    } else if ($('#id_emplphone').val()){
        var pattern = new RegExp($('#id_emplphone').attr("pattern"));
        if (!pattern.test($('#id_emplphone').val())){
            status_code_handler(4004);
            return false;
        }
    } else if (!$('#id_emplmail').val()){
        status_code_handler(4005);
        return false;
    } else {
        return true;
    }
}

My first three checks are working fine. When checking if the entered birthday is not in the future the function is returning a undefined result. I don't get why. The function gets stuck in this if clause. Following checks are not gonna be executed. Is there a mistake in nesting the ifs and else ifs?

Aucun commentaire:

Enregistrer un commentaire