简体   繁体   English

if陈述式传回true,而不是遍历我所有的if陈述式-JavaScript

[英]if statements returning true and not going over all of my if statements - javascript

Hi all my code works great when tested one by one but when i insert them all together if the first IF statement is reached it will return true and submit the form without validating the others.. can someone tell me how to change my code around so it will run all the IF statements before it returns true. 嗨,我的所有代码在一个接一个地测试时都很好用,但是当我将它们全部插入时,如果到达第一个IF语句,它将返回true并提交表格而不验证其他代码。有人可以告诉我如何更改我的代码吗?在返回true之前,它将运行所有IF语句。

Here is my code 这是我的代码

    if (this.element.find('#visitdate').length > 0) {
    var dateParts = $('#tvisitdate').val().split('/');
    var check = new Date(dateParts[2], dateParts[1]-1, dateParts[0], 0,0,0,0);
    var d = new Date();
    var today = new Date(d.getFullYear(), d.getMonth(), d.getDate());

    if (today.getTime() > check.getTime() ) {
        _errMsg = "Please enter a future visit date";
        return false;
    } else {
        return true;
    }
}

if (this.element.find('#birthdate').length > 0) {
    var dateParts1 = $('#birthdate').val().split('/');
    var check1 = new Date(dateParts1[2], dateParts1[1]-1, dateParts1[0], 0,0,0,0).getFullYear();
    var today1 = new Date();
    var year = today1.getFullYear();

    if (check1 >= year) {
        _errMsg = "Please enter a valid date of birthday";
        return false;
    } else {
        return true;
    }
}

Use a boolean (true/false) variable and then return that variable at the end of the code. 使用布尔(true / false)变量,然后在代码末尾返回该变量。 This way you only have one return statement: 这样,您只有一个return语句:

var check_no_error = true;
if (this.element.find('#visitdate').length > 0) {
    var dateParts = $('#tvisitdate').val().split('/');
    var check = new Date(dateParts[2], dateParts[1]-1, dateParts[0], 0,0,0,0);
    var d = new Date();
    var today = new Date(d.getFullYear(), d.getMonth(), d.getDate());

    if (today.getTime() > check.getTime() ) {
        _errMsg = "Please enter a future visit date";
        check_no_error = false;
    }
}

if (this.element.find('#birthdate').length > 0) {
    var dateParts1 = $('#birthdate').val().split('/');
    var check1 = new Date(dateParts1[2], dateParts1[1]-1, dateParts1[0], 0,0,0,0).getFullYear();
    var today1 = new Date();
    var year = today1.getFullYear();

    if (check1 >= year) {
        _errMsg = "Please enter a valid date of birthday";
        check_no_error = false;
    }
}
return check_no_error;

you should not use return for each if condition in this case better assign a boolean value for first if whether true or false . 在这种情况下,最好not use return for each if condition如果true或false,则最好为first分配一个布尔值。 if the value is true then process second if condition and change the value with true of false based on condition and return this value in the last you will be okay. 如果值为true,则处理第二个if条件,并根据condition更改为true或false的值,最后返回该值,您就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM