简体   繁体   English

无法使我的验证功能正常工作

[英]Can't get my validateage function to work

I have these two functions to calculate the persons age after they enter a date in the form MM/DD/YYYY. 我有这两个功能,可以在人们以MM / DD / YYYY格式输入日期后计算他们的年龄。 When it gets to the part if(age<21) and the age is under, it will alert the user with my message but will not return false. 如果到达部分if(age<21)并且年龄未满,它将用我的消息提醒用户,但不会返回false。 I just cant figure it out been working on it forever. 我只是想不出它一直在努力。 Thanks for any help! 谢谢你的帮助!

function validateAge()
{
    var x = /^\s*/;
    var datemsg = "";

    var inputDate = document.code.myDate.value;
    inputDate = inputDate.replace(x, "");
    document.code.myDate.value = inputDate;

    getAge(new Date(inputDate));

    return true;

}

function getAge(birth)
{

    var today = new Date();
    var nowyear = today.getFullYear();
    var nowmonth = today.getMonth();
    var nowday = today.getDate();

    var birthyear = birth.getFullYear();
    var birthmonth = birth.getMonth();
    var birthday = birth.getDate();

    var age = nowyear - birthyear;
    var age_month = nowmonth - birthmonth;
    var age_day = nowday - birthday;

    if(age_month < 0 || (age_month == 0 && age_day <0)) 
    {
            age = parseInt(age) -1;
     }
    //alert(age);

    if(age < 21)
    {
    alert("You are not of drinking age");
    return false;
    }


}

If you're correctly seeing the alert when you should then in the validateAge function change 如果您正确地看到警报,则应在validateAge函数中进行更改

getAge(new Date(inputDate));

return true;

to

return getAge(new Date(inputDate));

You'll also need to add 您还需要添加

return true;

to the end of your getAge() function so that it always returns a value. getAge()函数的末尾,以便它始终返回一个值。

You are not returning the result of getAge in the validateAge function ie validateAge always returns true. 您不会在validateAge函数中返回getAge的结果,即validateAge始终返回true。 You need to do something in validateAge with the return value of getAge eg 您需要在validateAge中使用getAge的返回值进行操作,例如

function validateAge()
{
    var x = /^\s*/;
    var datemsg = "";

    var inputDate = document.code.myDate.value;
    inputDate = inputDate.replace(x, "");
    document.code.myDate.value = inputDate;    

    return getAge(new Date(inputDate));    
}

function getAge(birth)
{
    var today = new Date();
    var nowyear = today.getFullYear();
    var nowmonth = today.getMonth();
    var nowday = today.getDate();

    var birthyear = birth.getFullYear();
    var birthmonth = birth.getMonth();
    var birthday = birth.getDate();

    var age = nowyear - birthyear;
    var age_month = nowmonth - birthmonth;
    var age_day = nowday - birthday;

    if(age_month < 0 || (age_month == 0 && age_day <0)) 
    {
        age = parseInt(age) -1;
    }
    //alert(age);

    if(age < 21)
    {
        alert("You are not of drinking age");
        return false;
    }
    else
    {
        return true;
    }
}

In your function validateage, you are calling the function getAge, but not doing anything based on the value it returns. 在函数验证中,您要调用函数getAge,但不会根据返回的值执行任何操作。 You are assuming, I believe, that the validateage function should end if getAge returns false. 我相信,您假设如果getAge返回false,validateage函数应该结束。 Rather the function is simply executing and then just ignores the return value, then goes to the next line and returns true. 而是函数只是简单地执行,然后忽略返回值,然后转到下一行并返回true。 Try: 尝试:

if (!getAge(new Date(inputDate))) {
return false;
} else {
return true;
}

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

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