Javascript 我如何验证某人自出生之日起已超过 18 岁?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7091130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:48:34  来源:igfitidea点击:

How can I validate that someone is over 18 from their date of birth?

javascriptasp.net

提问by Lakshmitha

I am doing validation for Driver's Date of birth, it should be minimum of 18 from the current date.

我正在验证司机的出生日期,它应该是当前日期的最低 18 日。

var Dates = $get('<%=ui_txtDOB.ClientID %>');   
var Split = Dates.value.split("/");

if (parseInt(Split[2]) > 1993) 
{
    alert("DOB year should be less than 1993");
    Dates.focus();
    return false;
}  

I am using this above JavaScript validation for checking a person's DOB above 18, but it is not correct. I need to check with today's date and it should be above 18. How can I compare and check with the current date?

我正在使用上面的 JavaScript 验证来检查 18 岁以上的人的 DOB,但它不正确。我需要检查今天的日期,它应该在 18 岁以上。如何与当前日期进行比较和检查?

回答by Curt

I think a better alternative would be to calculate the age of the user, and use that in your if statement.

我认为更好的选择是计算用户的年龄,并在您的 if 语句中使用它。

See this SO answer on how to do just that:

请参阅有关如何做到这一点的 SO 答案:

Calculate age in JavaScript

在 JavaScript 中计算年龄

回答by naveen

Try this.

尝试这个。

var enteredValue = $get('<%=ui_txtDOB.ClientID %>');;
var enteredAge = getAge(enteredValue.value);
if( enteredAge > 18 ) {
    alert("DOB not valid");
    enteredValue.focus();
    return false;
}

Using this function.

使用此功能。

function getAge(DOB) {
    var today = new Date();
    var birthDate = new Date(DOB);
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
        age--;
    }    
    return age;
}

Demo here: http://jsfiddle.net/codeandcloud/n33RJ/

演示在这里:http: //jsfiddle.net/codeandcloud/n33RJ/

回答by sdsf

<script>


    function dobValidate(birth) {


        var today = new Date();
        var nowyear = today.getFullYear();
        var nowmonth = today.getMonth();
        var nowday = today.getDate();
        var b = document.getElementById('<%=TextBox2.ClientID%>').value;



        var birth = new Date(b);

        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 > 100) {
            alert("Age cannot be more than 100 Years.Please enter correct age")
            return false;
        }
        if (age_month < 0 || (age_month == 0 && age_day < 0)) {
            age = parseInt(age) - 1;


        }
        if ((age == 18 && age_month <= 0 && age_day <= 0) || age < 18) {
            alert("Age should be more than 18 years.Please enter a valid Date of Birth");
            return false;
        }
    }



</script>

回答by bat020

After looking at various methods of doing this, I decided the simplest way was to encode the dates as 8-digit integers. You can then subtract today's code from the DOB code and check if it's greater than or equal to 180000.

在查看了执行此操作的各种方法后,我决定最简单的方法是将日期编码为 8 位整数。然后您可以从 DOB 代码中减去今天的代码并检查它是否大于或等于 180000。

function isOverEighteen(year, month, day) {
  var now = parseInt(new Date().toISOString().slice(0, 10).replace(/-/g, ''));
  var dob = year * 10000 + month * 100 + day * 1; // Coerces strings to integers

  return now - dob > 180000;
}

回答by Avtar Nanrey

let TODAY = new Date(Date.now());
let EIGHTEEN_YEARS_BACK = new Date(new Date(TODAY).getDate() + "/" + new Date(TODAY).getMonth() + "/" + (new Date(TODAY).getFullYear() - 18));
let USER_INPUT = new Date("2003/12/13");
// Validate Now
let result = EIGHTEEN_YEARS_BACK > USER_INPUT // true if over 18, false if less than 18

I think this is the closest possible way to check.

我认为这是最接近的检查方法。