For an order form I've created I've been asked to change it a bit so that if a certain shop is chosen they will not be able to select Sundays for a delivery date. My thinking was to have a script running that if a user selected that an alert would appear saying that it's not an option and the date field would be reset.
I have tried the following script but it does not work but no errors appear so I'm not sure where I have gone wrong:
const validation = dateString => {
var value = document.getElementById("collect").value;
const day = (new Date(dateString)).getDay();
if (value == "Auld Toon" && day == 0) {
return false;
}
return true;
}
// Sets the value to '' in case of an invalid date
document.querySelector('.date').onchange = evt => {
var value = document.getElementById("collect").value;
if (!validation(evt.target.value)) {
evt.target.value = '';
alert("Unfortunately " + value + " is shut on this day. Please select another");
}
}
This is the html for selecting a shop and choosing a date:
<select id="collect" name="collect">
<option value="" selected="selected" disabled>Select One</option>
<option value="Alford">Alford</option>
<option value="Auld Toon">Auld Toon</option>
<option value="Banff">Banff</option>
<option value="Emmas">Emmas</option>
<option value="Insch">Insch</option>
<option value="Kemnay">Kemnay</option>
<option value="Market Place">Market Place</option>
<option value="Mastrick"> Mastrick</option>
<option value="Meldrum Bakery">Meldrum Bakery</option>
<option value="North Street">North Street</option>
<option value="Rousay">Rousay</option>
<option value="Seafield Street">Seafield Street </option>
<option value="St Machar">St Machar </option>
<option value="St Swithin">St Swithin Street </option>
<option value="Stonehaven">Stonehaven</option>
<option value="Torry">Torry</option>
<option value="Keystore Old Aberdeen">Keystore Old Aberdeen</option>
<option value="Keystore Old Meldrum">Keystore Old Meldrum </option>
<option value="Highclere">Highclere</option>
</select>
<p>What date is this required for?</p>
<input id="datefield" class="date" name="date" type='date' onkeydown="return false" min='2019-05-10'></input>
Would appreciate any help as to where I have gone wrong and how I can go about fixing it. Thanks
Edit: Okay so it's because of this similar script running that stops my script working:
const validate = dateString => {
const day = (new Date(dateString)).getDay();
const month = (new Date(dateString)).getMonth()+1;
const number = (new Date(dateString)).getDate();
if (value == "Auld Toon" && day == 0 || number == 25 && month == 12 || number == 24 && month == 12 || number == 26 && month == 12 || number == 27 && month == 12 || number == 1 && month == 1 || number == 2 && month == 1) {
return false;
}
return true;
}
// Sets the value to '' in case of an invalid date
document.querySelector('.date').onchange = evt => {
if (!validate(evt.target.value)) {
evt.target.value = '';
alert("We cannot deliver butteries on this day. Please select another");
}
}
So is it possible to have these 2 scripts separate or would I just need to join them together and add an if statement for showing the appropriate alert?