在 HTML 5 输入类型日期上禁用周末 [英] Disable Weekends on HTML 5 input type date

查看:30
本文介绍了在 HTML 5 输入类型日期上禁用周末的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为 HTML 5 输入类型的日期禁用所有周末?

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" />

这篇关于在 HTML 5 输入类型日期上禁用周末的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆