选择特定日期时显示错误(日期输入)

时间:2019-07-04 10:32:31

标签: javascript html

我有以下输入内容允许用户选择订单中的日期:

users

我已经遵循了Tiffany Brown的教程,并且可以正常使用,所以在执行操作时出现错误:

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

功能noOrder(e){

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

}

date.addEventListener('input',noOrder);

但是当我尝试添加使其无法使用12月25日时,它将完全停止工作,并且不会出现任何错误。

这就是我正在做的:

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

功能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);

真正让我困惑的只是添加另一个变量,例如var month = weekDay(e.target.value).getUTCMonth()+ 1;整个事情停止了工作

有人能帮助我解决我的问题吗?

我已经以一种不同的方式进行了处理并使其起作用,我现在正在这样做:

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('');

}

0 个答案:

没有答案