Put age restriction of 18 years - Bootstrap Datepicker

江枫思渺然 提交于 2019-12-20 05:17:20

问题


I am using Bootstrap Datepicker.

I want to put an Age Restriction of 18 Years.

Dates less than Age 18 Years from current date should be disabled.

Here is my Fiddle:

http://jsfiddle.net/kGGCZ/17/

JS:

$(function()
{
    $('#datepicker').datepicker(
    {
        viewMode: "years",
        defaultDate: '-2yr', 
    }
    );
});

回答1:


Simply put endDate: '-18y' if you only want to allow user whose age is 18 years and above.

<input type="text" class="datepicker">

<script>
$('.datepicker').datepicker({
  endDate: '-18y'
});
</script>

Working Example: https://jsfiddle.net/sparshturkane/w46euf5q/1/

Please refer Bootstrap Date picker docs for more options and instructions




回答2:


This is the JavaScript you are after:

var dt = new Date();
dt.setFullYear(new Date().getFullYear()-18);

$('#datepicker').datepicker(
    {
        viewMode: "years",
        endDate : dt
    }
);

But your fiddle is using coffeescript so here is the same in coffee:

dt = new Date()
dt.setFullYear new Date().getFullYear() - 18
$("#datepicker").datepicker
  viewMode: "years"
  endDate: dt

working example: http://jsfiddle.net/Ln5x6/1/




回答3:


You can use yearRange, to let users choose only years which comes under 18+ from current year. Like,

jQuery('#dateob').datepicker({
changeMonth: true,
changeYear: true,
yearRange: '-110:-18',
showButtonPanel: true
});


来源:https://stackoverflow.com/questions/21880917/put-age-restriction-of-18-years-bootstrap-datepicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!