简体   繁体   English

jQuery检查日期选择器的年份

[英]jquery Check the year of a datepicker

i'm using the datepicker from jqueryui in a form. 我正在使用jqueryui中的datepicker。 Now i want to validate the year of birth. 现在,我想验证出生年份。 This means that my user can't put in a year that is higher than 1998 (he/she must be 18 years old). 这意味着我的用户输入的年份不能超过1998年(他/她必须年满18岁)。 How can i check if the value isn't higher than 1998? 如何检查该值是否不高于1998? This is what i've so far. 这就是我到目前为止。

<input type="text" id="birthyear" required><br />

$(function(){
    $('#birthyear').datepicker({
        minDate: new Date(1900,1-1,1),
        defaultDate: new Date(1991,1-1,1),
        changeYear: true,
        yearRange: '-110:-18'
    });
});

You can get the Date object from the datepicker using getDate: 您可以使用getDate从datepicker获取Date对象:

var date = $("#birthyear").datepicker("getDate");

From there you can get the year: 从那里您可以得到年份:

var year = date.getFullYear();

If you want to make sure the user is at least 18 years old, you might want to check more than just the year: you'll also want to make sure today's date is after their 18th birthday. 如果要确保用户至少18岁,则可能需要检查的不只是年份:还需要确保今天的日期在他们的18岁生日之后。

There's a few ways I can see to achieve this 我可以通过几种方法实现这一目标

  1. Set a max cut-off date to prevent anyone from even selecting a date past your determined 设置最大截止日期,以防止任何人甚至选择超出您确定的日期的日期

     $('#birthyear').datepicker({ minDate: new Date(1900,1-1,1), defaultDate: new Date(1991,1-1,1), changeYear: true, yearRange: '-110:-18', maxDate: new Date(1997, 11, 31) }); 
  2. Bind a validation function to the onChangeMonthYear event on initialisation 初始化时将验证函数绑定到onChangeMonthYear事件

     $('#birthyear').datepicker({ minDate: new Date(1900,1-1,1), defaultDate: new Date(1991,1-1,1), changeYear: true, yearRange: '-110:-18', onChangeMonthYear: function(year) { if (year > 1998) ; // alert user of error } }); 
  3. A little trickier, but if you've got some client-side validation function already, you can get the Date object like this: 有点棘手,但是如果您已经有了一些客户端验证功能,则可以像下面这样获取Date对象:

     $('#birthyear').datepicker('getDate').getFullYear() 

Hope that helps 希望能有所帮助

I see some of the other answers are showing you how to confirm the year but years pass by. 我看到其他一些答案正在向您展示如何确认年份,但年份过去了。 I had a similar requirement for a project that required the users age to be verified and rather than hard coding a year, and though you are requesting to check the year(but with the age requirements in parentheses) I would suggest a more dynamic and maintainable solution as provided below. 对于一个需要验证用户年龄而不是对年份进行硬编码的项目,我有类似的要求,尽管您要求检查年份(但在括号中带有年龄要求),我还是建议您更动态,更易于维护解决方案如下。

    //Initialize datepicker
         $(function() {
                $( "#dob" ).datepicker({
                    changeMonth: true,//allow month select
                    changeYear: true,//allow year select
                    yearRange: "-100:+0",//set range of year select
                    dateFormat: 'MM dd, yy',//set format of "displayed element"
                    altField: "#hidden-dob",//set alt field "hidden element"
                    altFormat: "yy/mm/dd",//set format for hidden element
                    onClose: function() {
                        $('#hidden-age').val(getUserAge($('#hidden-dob').val()));
                    }
                });
            });
    //year/month/day

//Pass the date to retrieve the users age FORMAT <yy/mm/dd> "The function that you need"
            function getUserAge(dateString) {
                var today = new Date();
                var dob = new Date(dateString);
                var age = today.getFullYear() - dob.getFullYear();
                var month = today.getMonth() - dob.getMonth();
                if (month < 0 || (month === 0 && today.getDate() < dob.getDate())) {
                    age--;
                }
                return age;
            }

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

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