You can do all the calculations in the onSelect:fn(){}
provided by the the datepicker itself. You can get the selected value from the function's param name of your choice, i have given it the name date
. Now when you get the date from here you can convert it to a dateObject to extract the year from it, same way you need to get the current year with a new date object.
$(function() {
$("#datepicker").datepicker({
changeMonth: true,
changeYear: true,
yearRange: '-115:+0M',
onSelect: function(date) { // bind the builtin onSelect event
// which gets you the selected date
var selYear = new Date(date).getFullYear(); // get the full year of selected date
var currYear = new Date().getFullYear(); // get the current year
if ((currYear - selYear) > 10) { // check in the if conditon for 10 years
$('#display_error').html('You should choose dates from last 10 years only.');
this.value='';
}
}
});
});
<link target='_blank' href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<p>Date:
<input type="text" id="datepicker">
</p>
<label id="display_error" class="display_error"></label>
I found that in JavaScript, switch is always a sentence of instruction and using it as an expression will give an error. So when I have to assign different values to a variable according to different ...
I found that in JavaScript, switch is always a sentence of instruction and using it as an expression will give an error. So when I have to assign different values to a variable according to different ...
I actually load an external JavaScript on my page this way: <script type='text/javascript' src='http://somedomain.com/somescript.php'></script> Note: it is a PHP file, but it produces ...
I actually load an external JavaScript on my page this way: <script type='text/javascript' src='http://somedomain.com/somescript.php'></script> Note: it is a PHP file, but it produces ...
I'm trying to get count value from a function and want to store in outside of the function. var count; client.count({ index: 'employee', type: 'details', ...
I'm trying to get count value from a function and want to store in outside of the function. var count; client.count({ index: 'employee', type: 'details', ...
I have this code here: <!DOCTYPE html> <html> <body> <p id="demo">Display the result here.</p> <input type = "text" value = "ood" id = "txt1"/> <script> ...
I have this code here: <!DOCTYPE html> <html> <body> <p id="demo">Display the result here.</p> <input type = "text" value = "ood" id = "txt1"/> <script> ...