Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JQuery查找一个人是否未满18岁_Jquery_Html_Validation - Fatal编程技术网

使用JQuery查找一个人是否未满18岁

使用JQuery查找一个人是否未满18岁,jquery,html,validation,Jquery,Html,Validation,当我在输入字段中输入值时,我想确定此人是否小于18岁。为此,我创建了一个日期输入框并创建了一个函数。我的功能不能正常工作。我做错了什么?我怎样才能使这项工作成功 <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, init

当我在输入字段中输入值时,我想确定此人是否小于18岁。为此,我创建了一个日期输入框并创建了一个函数。我的功能不能正常工作。我做错了什么?我怎样才能使这项工作成功

    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title>Birthday Validation</title>
      <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
      <link rel="stylesheet" href="/resources/demos/style.css">
      <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

    <script>

        $(document).ready(function () {
                $("#DOB").on("change paste keyup", function() {


                $datestring = $(this).val();
                //alert(jQuery.type(Date($datestring)));       

                //$eightYearsAgo = now() - $birthday;
                $birthday = $.datepicker.formatDate('yy/mm/dd', new Date()) - Date($datestring);


                if(!birthday.isValid()){
                     alert("INVALID DATE");
                }else if (eightYearsAgo.isAfter(birthday)){
                     alert("18+");
                }else{
                     alert("< 18");
                }

            });
        }); 
    </script>

    </head> 


    <body>

    <input name="DOB" type="date" class="form-control" id="DOB" required  placeholder="MM/DD/YYYY" onchange="validate(date)"> 


    </body>
    </html>

我已经这样做了,你需要使用正确的日期选择器功能来让它工作

代码如下:

$(document).ready(function () {
    $("#age").datepicker({
        onSelect: function(value, ui) {
            var current = new Date().getTime(), 
                dateSelect = new Date(value).getTime();
                age = current - dateSelect;
                ageGet = Math.floor(age / 1000 / 60 / 60 / 24 / 365.25); // age / ms / sec / min / hour / days in a year
            if(ageGet < 18){
                less_than_18(ageGet);
            }else{
                greater_than_18(ageGet);
            }
        },
        yearRange: '1900:+0d',//base year:current year
        changeMonth: true,
        changeYear: true,
        defaultDate: '-18yr',
    }).attr("readonly", "readonly"); //prevent manual changes


    function less_than_18(theAge){
        alert("Failed! your age is less than 18. Age: "+theAge);
    }
    function greater_than_18(theAge){
        alert("Done! your age is greater or equal to 18. Age: "+theAge);
    }
});

您可以在此处进行测试:

您可能必须为此过滤器设置“两个日期之间”浏览器的开发控制台告诉您正在尝试使用从未定义的变量。您没有一个变量,但您引用的许多变量和函数都不存在。嗨,TriForce,感谢您的回复。只是好奇你是怎么得到datepicker功能的?