Reputation: 9
I am trying to disable previous dates in calendar. I am using this code like
My HTML code is below.
<input type="text" required name="date_from" name="date_from" class="mydate input-text full-width" placeholder="Departure Date" />
My script code.
<script type="text/javascript">
$(".mydate").datepicker({
format:'yyyy-mm-dd',
autoclose: true
});
$(".flexslider").flexslider({
animation: "fade",
controlNav: false,
animationLoop: true,
directionNav: false,
slideshow: true,
slideshowSpeed: 5000
});
</script>
It shows calendar without previous dates disabled.
Upvotes: 0
Views: 18809
Reputation: 1
Try this code... this will restrict the user if user submits form with date less than today's date then shows alert message to change the date.
HTML Code
<form name="myform" onsubmit="return validateDateOfAppointment()">
<input type="date" name="Date of Appointment" placeholder="Date of Appointment" id="Date" />
</form>
JavaScript Code
function validateDateOfAppointment(){
var date=document.getElementById("Date").value;
var d=new Date();
var x=d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();
var checkDate=date.substr(8,2);
var equalDate=d.getDate();
var checkMonth=date.substr(5,2);
var equalMonth=d.getMonth();
var checkYear=date.substr(0,4);
var equalYear=d.getFullYear();
if(checkMonth>=equalMonth){
if(checkDate<equalDate){
alert("Date cannot be less than today!! ");
return false;
}
}
else if(checkMonth<equalMonth){
if(checkYear<equalYear){
alert("Date cannot be less than today!! ");
return false;
}
}
}
Upvotes: 0
Reputation: 2352
Use bootstrap date picker
. These are files to include
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/css/datepicker.css" rel="stylesheet" type="text/css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.js"></script>
Use this code to disable previous dates
HTML Input field
<input id="date" data-provide="datepicker" name="date_from" >
JAVASCRIPT
var date = new Date();
date.setDate(date.getDate());
$('#date').datepicker({
startDate: date
});
Upvotes: 1
Reputation: 16436
As from your format may be you are using bootstrap date picker. So use startDate
to disable previous dates
$(".mydate").datepicker({
format:'yyyy-mm-dd',
startDate: new Date(),
autoclose: true
});
Upvotes: 0
Reputation: 1786
Set minDate in initialization on datepicker
minDate:new Date()
$(".mydate").datepicker({
format:'yyyy-mm-dd',
autoclose: true,
minDate:new Date()
});
Upvotes: 0
Reputation: 429
You need to set minDate option while applying datepicker.
Try This,
<script type="text/javascript">
$(".mydate").datepicker({
format:'yyyy-mm-dd',
autoclose: true,
minDate: 0,
});
</script>
Upvotes: 0
Reputation: 154
You have to set the minDate option. Example + fiddle on this forum:
jQuery Date Picker - disable past dates
Upvotes: 0