asp.net 我怎样才能证明某人从出生之日起已满18岁?

2j4z5cfb  于 5个月前  发布在  .NET
关注(0)|答案(9)|浏览(67)

我正在验证驾驶员的出生日期,它应该是从当前日期的最低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;
}

字符串
我正在使用上述JavaScript验证来检查某人的出生日期是否在18岁以上,但它不正确。我需要检查今天的日期,它应该在18岁以上。我如何比较和检查当前的日期?

bbuxkriu

bbuxkriu1#

我认为一个更好的选择是计算用户的年龄,并在if语句中使用它。
看看这个关于如何做到这一点的答案:
Calculate age in JavaScript

8fsztsew

8fsztsew2#

试试这个.

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

字符串
使用此功能。

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;
}


在此演示:http://jsfiddle.net/codeandcloud/n33RJ/

2g32fytz

2g32fytz3#

在研究了各种方法之后,我决定最简单的方法是将日期编码为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;
}

字符串

6ljaweal

6ljaweal4#

我的方法是找到从今天算起18年(或任何数字)前的日期,然后看看这是否在他们的dob之后(大于)。通过将所有值设置为date对象,它使比较变得容易。

function is_of_age(dob, age) {
  // dates are all converted to date objects
  var my_dob = new Date(dob);
  var today = new Date();
  var max_dob = new Date(today.getFullYear() - age, today.getMonth(), today.getDate());
  return max_dob.getTime() > my_dob.getTime();
}

字符串
因为Date对象可以解析各种格式的字符串,所以你不必太担心dob是从哪里来的。只需调用is_of_age(“1980/12/4”,18);或is_of_age(“2005-04-17”,13);或基本上任何可以解析为Date参数的字符串格式或数字。

dl5txlt9

dl5txlt95#

<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>

字符串

nvbavucw

nvbavucw6#

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

字符串
我想这是最接近的检查方法了。

bjp0bcyl

bjp0bcyl7#

您可以使用以下:

var birthDate = new Date("2018-06-21");
var birthDate1 = new Date("1975-06-21");
 function underAgeValidate(birthday) {
    const diff = Date.now() - birthday.getTime();
    const ageDate = new Date(diff);
    let age = Math.abs(ageDate.getUTCFullYear() - 1970);
      return age < 18;
  };

console.log(underAgeValidate(birthDate));
console.log(underAgeValidate(birthDate1));

字符串

k5ifujac

k5ifujac8#

我最喜欢的方法是这样的:

var dateOfBirth = new Date("02/23/1900");
// calculate difference between now and the dateOfBirth (in milliseconds)
var differenceMs = Date.now() - dateOfBirth.getTime();
// convert the calculated difference in date format
var dateFromEpoch = new Date(differenceMs); 
// extract year from dateFromEpoch
var yearFromEpoch = dateFromEpoch.getUTCFullYear();
// calculate the age of the user
var age = Math.abs(yearFromEpoch - 1970);

console.log("Age of the user: " + age + " years")

字符串

k0pti3hp

k0pti3hp9#

找不到任何简单的解决方案,实际上它应该工作,它不是某种矫枉过正(换句话说,有高“认知复杂性”),也计算age代替(因为它是在接受的答案)不是一个选项,在我的情况下。
弄清楚这样的事情:

const exactlyNYearsAgoDate = (yearsAgo) => new Date(
  new Date().setFullYear(new Date().getFullYear() - yearsAgo)
)

console.log(exactlyNYearsAgoDate(18))

字符串

为什么new Date()用了3次?

  1. new Date().getFullYear() - yearsAgo-仅返回年份(例如2005),我的引导也传递yearsAgo - 1(例如17 yo可能按年份有效,即使他们今年还没有生日)。
  2. new Date().setFullYear(...)-这将基本上改变当前日期的年份,但此操作的返回将在milliseconds中。
    1.(可选)new Date(...)-将milliseconds解析为Date对象,这将导致日期确切n年前(n是我们的yearsAgo)。

使用普通JS验证:

const exactlyNYearsAgoDate = (yearsAgo) => new Date(
  new Date().setFullYear(new Date().getFullYear() - yearsAgo)
)
const mockBirthday = new Date('2000-07-15T00:01:09.420Z')
const isAdult = mockBirthday.getTime() < exactlyNYearsAgoDate(18).getTime()

console.log('isAdult:', isAdult)

使用Zoddocs)验证:

z.date().max(exactlyNYearsAgoDate(18), {message: 'You are not 18 years old'})

相关问题