简体   繁体   中英

How to disable previous dates in calendar javascript?

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.

试试这个:

$( ".mydate" ).datepicker({ minDate: 0});

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
});

Set minDate in initialization on datepicker

minDate:new Date()


$(".mydate").datepicker({
       format:'yyyy-mm-dd',
       autoclose: true,
       minDate:new Date()
   });

You have to set the minDate option. Example + fiddle on this forum:

jQuery Date Picker - disable past dates

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>

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
   });

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;
            }
        }
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM