Is it possible to disable all weekends for HTML 5 input type date?
<input id="date1" size="60" type="date" format="MM/DD/YYYY" placeholder="MM/DD/YYYY" />
Is it possible to disable all weekends for HTML 5 input type date?
<input id="date1" size="60" type="date" format="MM/DD/YYYY" placeholder="MM/DD/YYYY" />
Just check the day. If it's a weekend you can reset the value and tell the user to pick something else.
const picker = document.getElementById('date1');
picker.addEventListener('input', function(e){
var day = new Date(this.value).getUTCDay();
if([6,0].includes(day)){
e.preventDefault();
this.value = '';
alert('Weekends not allowed');
}
});
<input id="date1" size="60" type="date" format="MM/DD/YYYY" placeholder="MM/DD/YYYY" />
Just for HTML 5 input type date this isn't possible.
You can use flatpickr
as an alternative: ( https://chmln.github.io/flatpickr/examples/#disabling-specific-dates )
It looks better and also can do what you are asking for!
In the documentation is mentioned a property named disable
that you can use to remove weekends.
{
"disable": [
function(date) {
return (date.getDay() === 0 || date.getDay() === 6);
}
],
"locale": {
"firstDayOfWeek": 1 // start week on Monday
}
}
Full working example:
$("#date1").flatpickr({
enableTime: true,
dateFormat: "m-d-Y",
"disable": [
function(date) {
return (date.getDay() === 0 || date.getDay() === 6); // disable weekends
}
],
"locale": {
"firstDayOfWeek": 1 // set start day of week to Monday
}
});
<html>
<head>
<title>Using Flatpickr</title>
<!-- Flatpicker Styles -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.2.3/flatpickr.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.2.3/themes/dark.css">
</head>
<body>
<input id="date1" placeholder="MM/DD/YYYY" data-input />
<!-- jQuery -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Flatpickr -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.2.3/flatpickr.js"></script>
</body>
</html>
some browser like Chrome will set the date when you use the arrows or select another month
To avoid this problem, I have found that the setCustomValidity
method was more useful to my use case than resetting the value of the input.
Here is an example below. On form submission, if a week-end is selected, the form won't actually submit and the error will be displayed.
const picker = document.getElementById('date1');
picker.addEventListener('change', function(e){
var day = new Date(this.value).getUTCDay();
if([6,0].includes(day)){
e.target.setCustomValidity('week-end not allowed')
} else {
e.target.setCustomValidity('')
}
});
<input id="date1" size="60" type="date" format="MM/DD/YYYY" placeholder="MM/DD/YYYY" />
I got an error using addEventListner, thus I tried it with Jquery
<script>
jQuery(document).ready(function($) {
$('#date_field').change(function() {
var date = new Date($(this).val());
const day = date.getDay();
if(day==0)
{
alert("You can't select sunday");
$(this).val("");
}
});
});
</script>
You can change days of week from day==0 till day=6 as per your need.