0

I have the following input for allowing users to select a date in an order form:

<input id="datefield" name="date" type='date' required onkeydown="return false" min='2019-05-10'></input>

I've followed that Tiffany Brown tutorial and I've got it working so an error comes up by doing:

var date = document.querySelector('[type=date]');

function noOrder(e){

var weekDay = new Date( e.target.value ).getUTCDay();

// Days in JS range from 0-6 where 0 is Sunday and 6 is Saturday

if( weekDay == 0){

    e.target.setCustomValidity('Unfortunately we cannot deliver cakes on Sundays, please select another day');


} else {

    e.target.setCustomValidity('');

}

}

date.addEventListener('input',noOrder);

but when I try to add to it so that the 25th December is unavailable it just stops working completely and no errors appear.

This is what I am doing:

var date = document.querySelector('[type=date]');

function noOrder(e){

var weekDay = new Date( e.target.value ).getUTCDay();
 var month = weekDay(e.target.value).getUTCMonth()+1;
 var day = weekDay(e.target.value).getUTCDate();

// Days in JS range from 0-6 where 0 is Sunday and 6 is Saturday

if( weekDay == 0){

    e.target.setCustomValidity('Unfortunately we cannot deliver cakes on Sundays, please select another day');

}else if (day == 25 && month == 12) {
 e.target.setCustomValidity('Unfortunately we cannot deliver cakes on this day, please select another day');

} else {

    e.target.setCustomValidity('');

}

}

date.addEventListener('input',noOrder);

What really confuses me is just by adding another variable, eg var month = weekDay(e.target.value).getUTCMonth()+1; the whole thing stops working

Is anyone able to help me with where I am going wrong?

Have gone about it a different way and have it working, I am now doing:

const validate = dateString => {
  const day = (new Date(dateString)).getDay();
  const month = (new Date(dateString)).getMonth()+1;
  const number = (new Date(dateString)).getDate();
  if (number == 25 && month == 12) {
    return false;
  }
  return true;
}

// Sets the value to '' in case of an invalid date
document.querySelector('input').onchange = evt => {
  if (!validate(evt.target.value)) {
    evt.target.value = '';
    alert("We cannot deliver cakes on this day. Please select another");

  }
}
Lewis Ross
  • 153
  • 2
  • 13
  • 1
    Also, well details post about it: https://tiffanybbrown.com/2013/10/24/date-input-in-html5-restricting-dates-and-thought-for-working-around-limitations/ – Mosh Feu Jul 04 '19 at 10:37
  • No. You may use https://tiffanybbrown.com/2013/10/24/date-input-in-html5-restricting-dates-and-thought-for-working-around-limitations/ work around for showing only certain dates. – Ramesh Jul 04 '19 at 10:42
  • okay thanks, I'll have a look – Lewis Ross Jul 04 '19 at 10:53
  • @MoshFeu are you able to have a look and see where I might be going wrong. Because even I just add var month = weekDay(e.target.value).getUTCMonth()+1; to the original working code, the whole thing stops working which I don't understand why that would be the case – Lewis Ross Jul 04 '19 at 15:44
  • @Ramesh do you have any ideas? Any help would be much appreciated – Lewis Ross Jul 04 '19 at 15:45
  • @LewisRoss I can try. What is `weekDay`? BTW, you can reach me via one of my channels in my [profile](https://stackoverflow.com/users/863110/mosh-feu?tab=profile) – Mosh Feu Jul 04 '19 at 16:32
  • Much appreciated, the variable weekDay is for the day number I. E Sunday =0, Monday =1 etc – Lewis Ross Jul 04 '19 at 21:41
  • can this be removed as a duplicate? because I don't think it is – Lewis Ross Jul 05 '19 at 10:38
  • Have gone about it a different way and now have it working – Lewis Ross Jul 05 '19 at 10:58

0 Answers0