admin管理员组

文章数量:1415697

I am using Html5 date picker in my project of room reservation system please anybody help me to validate this date picker. Validation must include disabling dates earlier than today's date disabling dates which have been selected earlier for rooms.

    <html>
<body>
<form name="f2" action="payment.jsp" method="get" >
            <label> Date:</label><br>
            From:<input type="date" name="from" id="myDate" value="demo">
            Till:<input type="date" name="till" id="myDate1" value="demo1"><br>
  
 <input type="submit" onclick="myFunction()">
 <script>
    function myFunction() {
    var x = document.getElementById("myDate").value;
    document.getElementById("demo").innerHTML = x;
     var y = document.getElementById("myDate1").value;
    document.getElementById("demo1").innerHTML = y;
}      
</script>
  </body>
  </html>

I am using Html5 date picker in my project of room reservation system please anybody help me to validate this date picker. Validation must include disabling dates earlier than today's date disabling dates which have been selected earlier for rooms.

    <html>
<body>
<form name="f2" action="payment.jsp" method="get" >
            <label> Date:</label><br>
            From:<input type="date" name="from" id="myDate" value="demo">
            Till:<input type="date" name="till" id="myDate1" value="demo1"><br>
  
 <input type="submit" onclick="myFunction()">
 <script>
    function myFunction() {
    var x = document.getElementById("myDate").value;
    document.getElementById("demo").innerHTML = x;
     var y = document.getElementById("myDate1").value;
    document.getElementById("demo1").innerHTML = y;
}      
</script>
  </body>
  </html>

Share Improve this question asked Oct 17, 2016 at 9:48 Saquib AliSaquib Ali 1201 gold badge2 silver badges11 bronze badges 4
  • shouldn't demo and demo1 values be actual dates? like 2016-10-17? – andrew Commented Oct 17, 2016 at 9:50
  • also, take a look on this post: stackoverflow./questions/8574442/…. It has some tips you can use – andrew Commented Oct 17, 2016 at 9:51
  • It has been already discussed in stack.Please Refer – Thala Commented Oct 17, 2016 at 9:54
  • Possible duplicate of JavaScript to validate date – roberrrt-s Commented Oct 17, 2016 at 9:58
Add a ment  | 

1 Answer 1

Reset to default 2

You don't need JS to acplish that: There is attr min on html5:

<input type="date" name="from" id="myDate" value="demo" min="2016-10-13">

To update it to the current date, I use todayDate():

$(document).ready(function(){
    $('#myDate').attr('min', todayDate());
});

function todayDate() {
    var today = new Date(); // get the current date
    var dd = today.getDate(); //get the day from today.
    var mm = today.getMonth()+1; //get the month from today +1 because january is 0!
    var yyyy = today.getFullYear(); //get the year from today

    //if day is below 10, add a zero before (ex: 9 -> 09)
    if(dd<10) {
        dd='0'+dd
    }

    //like the day, do the same to month (3->03)
    if(mm<10) {
        mm='0'+mm
    }

    //finally join yyyy mm and dd with a "-" between then
    return yyyy+'-'+mm+'-'+dd;
}
$(document).ready(function(){
    $('#myDate').attr('min', todayDate());
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<body>
<form name="f2" action="payment.jsp" method="get" >
            <label> Date:</label><br>
            From:<input type="date" name="from" id="myDate" value="demo" min="2016-10-13">
            Till:<input type="date" name="till" id="myDate1" value="demo1"><br>
  
  </body>
  </html>

本文标签: javascriptValidation of Html 5 DatepickerStack Overflow