I have an HTML script with date inputs for each day of the week (Sunday-Saturday). I’d like to validate the inputs so that the user can only select dates which match that day of the week. For example, December 1, 2024 was a Sunday, so the user can only input December 1, 8, 15, 22, etc into that box. They cannot input December 2 or 3.
<table>
<tbody>
<tr>
<td><label for="date">Sunday:</label></td>
<td><label for="date">Monday:</label></td>
<td><label for="date">Tuesday:</label></td>
<td><label for="date">Wednesday:</label></td>
<td><label for="date">Thursday:</label></td>
<td><label for="date">Friday:</label></td>
<td><label for="date">Saturday:</label></td>
</tr>
<tr>
<td><input name="date" type="text" style="width:130px" onfocus="(this.type='date')" onblur="if(!this.value){this.type='text';this.style.width='130px'}"></td>
<td><input name="date" type="text" style="width:130px" onfocus="(this.type='date')" onblur="if(!this.value){this.type='text';this.style.width='130px'}"></td>
<td><input name="date" type="text" style="width:130px" onfocus="(this.type='date')" onblur="if(!this.value){this.type='text';this.style.width='130px'}"></td>
<td><input name="date" type="text" style="width:130px" onfocus="(this.type='date')" onblur="if(!this.value){this.type='text';this.style.width='130px'}"></td>
<td><input name="date" type="text" style="width:130px" onfocus="(this.type='date')" onblur="if(!this.value){this.type='text';this.style.width='130px'}"></td>
<td><input name="date" type="text" style="width:130px" onfocus="(this.type='date')" onblur="if(!this.value){this.type='text';this.style.width='130px'}"></td>
<td><input name="date" type="text" style="width:130px" onfocus="(this.type='date')" onblur="if(!this.value){this.type='text';this.style.width='130px'}"></td>
</tr>
<tr>
<table>
This script where getDay()
returns the day of the week (from 0 to 6) of a date.
<p id="date1"></p>
const weekday = new Date("December 01, 2024 01:15:00");
let day = weekday.getDay()
document.getElementById("date1").innerHTML = day;
The script below returns an alert every time a date selected (including Sundays). And if I add else
it does not return an alert at all (including Mondays-Saturdays). However I may be inserting the else
incorrectly.
function validateDate(date) {
const selectedDate = new Date(date);
const day = selectedDate.getDay();
if (day !== 0) {
alert("Please select a date that falls on a Sunday.");}
}
Thank you in advance
2
Answers
Click validate all to check wrong inputs.
Use the date step attribute
The question raises an interesting feature of date inputs that is worth knowing.
Most browsers support the step attribute on date inputs. And this can be used with the min attribute to limit user input to specific weekdays. By using reportValidity and the pseudo class :invalid, one can create a validating weekday input with minimal code.
(Firefox)
Adding min and step values disables other weekdays.
(Chrome)
Snippet
Much of the snippet is to support OP’s cumbersome custom control. Yet, only a minimal amount of this is needed to do a plain weekday date input.