mandiatutti
mandiatutti

Reputation: 57

Set a maximum year in datepicker jquery

I have the following code

<script>
        $(document).ready(function() {
            $(".datepicker").datepicker( {
        year = today.getFullYear,
        format: " yyyy", // Notice the Extra space at the beginning
        viewMode: "years", 
        minViewMode: "years",
        yearRange: '1920 : year',
        autoclose: true,            
        });
        });
    </script>

as you can see I only need the year, but I don't want to pick the following years... I mean 2020, 2021, 2022... Shouldn't be even able to be picked.. I looked around but couldn't find a solution, can someone help me with that? Thanks

EDIT:

<script>
$(document).ready(function() {
  $('#datepicker').keyup(function() {

    var inputVal = $(this).val();
    var dateReg = /^\d{4}$/;

    if (dateReg.test(inputVal)) {
      $("#datemsg").html("Valid").css("color", "green");
    } else {

      $("#datemsg").html("Invalid date").css("color", "red");
    }
  });

  $("#datepicker").datepicker({
    format: " yyyy", // Notice the Extra space at the beginning
    viewMode: "years",
    minViewMode: "years",
    autoclose: true,
    endDate: new Date(),
  });
});
</script>

Upvotes: 1

Views: 757

Answers (1)

freeek
freeek

Reputation: 978

You need to concatenate year to your range string, cause js won't parse this variable:

    year = today.getFullYear,
    $(".datepicker").datepicker({
        format: " yyyy", // Notice the Extra space at the beginning
        viewMode: "years", 
        minViewMode: "years",
        yearRange: '1920 : ' + year,
        autoclose: true,            
     });

Upvotes: 1

Related Questions