Search code examples
jqueryjquery-uidatepickerjquery-ui-datepicker

jQuery UI datepicker validation based on the year


I need to get an error when the user selects the year 2016 in my calendar UI date-picker. I'm using jQuery plugins for error messages. This is my script:

$(document).ready(function(){
    $("#datepicker").click(function(){

        $("#datepicker").datepicker({
            changeMonth: true,
            changeYear: true,
            yearRange: "1930:2016",
            dateFormat : 'dd-mm-yy'
        });

        $( "#datepicker" ).datepicker("show");
    });
});

This is my HTML:

<div class="row">
    <div class="col-sm-4">
        <div class="form-group field-user-user_dob has-success">
            <label class="control" for="user-user_dob">Date of Birth</label>
            <input type="text" id="datepicker" class="form-control" name="datepicker" readonly size="12">
            <div class="help-block"></div>
        </div>
    </div>
</div>

Solution

  • You can find the selected year on the datepicker using the solution written here. After finding the date you can check that and throw an error message as well.

    In another sense, you can get the total value and extract the year from the value (as you are defining the deatFormat). And you can through error depending on that.

    Hope it helps..:)