Put age restriction of 18 years - Bootstrap Datepicker

后端 未结 3 609
攒了一身酷
攒了一身酷 2021-01-24 11:55

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.

相关标签:
3条回答
  • 2021-01-24 12:13

    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

    0 讨论(0)
  • 2021-01-24 12:15

    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/

    0 讨论(0)
  • 2021-01-24 12:28

    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
    });
    
    0 讨论(0)
提交回复
热议问题