I have an input field with type="time" . How to make this field accept only the current or future?
If the user enters the wrong time, a message should be displayed asking you to enter the time again. The future here means up to 2 days.
Here I just show the current time using jquery.
function validDate(){ var today = new Date().toISOString().split('T')[0]; document.getElementsByName("date")[0].setAttribute('min', today); } $(function() { $('input[type="time"][value="now"]').each(function() { var d = new Date(), h = d.getHours(), m = d.getMinutes(); if (h < 10) h = '0' + h; if (m < 10) m = '0' + m; $(this).attr({ 'value': h + ':' + m }); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body onload="validDate()"> <div class="form-group"> <p>Date<span>*</span></p> <input type="date" name="date" id="date" class="form-control input-sm " required /> </div> <div> <p>Time <span>*</span></p> <input type="time" value="now" name="time" id="time" class="form-control input-sm " required /> </div> </body>
source share