用户输入年月日,判断是否大于18岁

用的函数是setFullYear(),getFullYear()

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../common/jquery-1.8.3.min.js"></script>
</head>
<body>
    <div>
        <label for="">出生年</label>
        <input type="text" id="year">
    </div>
    <div>
        <label for="">出生月</label>
        <input type="text" id="month">
    </div>
    <div>
        <label for="">出生日</label>
        <input type="text" id="date">
    </div>
    <div>
        <button id="btn">提交</button>
    </div>
</body>
<script>
    $("#btn").click(function(){
        var year = $("#year").val();
        var month = $("#month").val();
        var date = $("#date").val();
        if(year == '' && month == '' && date == ''){
            alert("请输入相应的出生年月日");
            return;
        }
        var myDate = new Date();
        myDate.setFullYear(year ,month-1 , date);
        var nowDate = new Date();
        console.log(nowDate.getFullYear()-18);
        nowDate.setFullYear(nowDate.getFullYear()-18);
        if((nowDate - myDate) < 0 ){
            alert("年龄不满18岁");
        }
        alert("提交成功");
        return true;
    })


</script>
</html>

  

posted @ 2018-11-21 17:58  董七  阅读(957)  评论(0编辑  收藏  举报