如何验证datepicker禁止/拒绝某些日期? [英] How to validate datepicker to forbid/reject certain dates?

查看:431
本文介绍了如何验证datepicker禁止/拒绝某些日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我正在使用的日历控制。用户可以从日历中选择任何日期。我需要验证星期六和星期日以及1/1和1/25的日期。如果他们选择这些天,如果它不是有效的日期,我需要向他们显示一个弹出消息。

解决方案

尝试这个: p>

  $(input [id ^ ='Date-<%= Model.ID%>'] function(){
var date = new Date($(this).val())。getDay();
if(date == 0 || date == 6){
alert('weekend');
}
});

这是设置为更改事件。您的活动可能会有所不同。



它的输入值为: $(this).val()



从中创建一个新的 Date 对象: new Date($(this)) val())



然后获取日数: .getDay() which返回值为0到6,其中0为星期日,6为星期六。



然后您只需测试0或6。



实例: http://jsfiddle.net/UwcLf/






编辑: Nick Craver 可以禁用特定日期,如果您不需要为周末选择运行备用代码。



从尼克的链接答案:在jQuery UI datepicker上禁用一周中的特定日期

  $(input [id ^ ='Date-<%= Model.ID%>']])。datepicker({ 
beforeShowDay:function(date){
var day = date.getDay();
return [(day!= 0&& day!= 6)];
}
});

更新示例: http://jsfiddle.net/UwcLf/1/



为残疾日添加阵列: http://jsfiddle.net/UwcLf/3/


I have this Calendar Control I am using. A user can select any date from the calendar. I need to validate the dates like Saturday and Sundays and 1/1 and 1/25. If they select these days I need to show them a Popup message if it's not a valid date.

解决方案

Try this:

$("input[id^='Date-<%=Model.ID%>']").change(function() {
    var date = new Date($(this).val()).getDay();
    if(date == 0 || date == 6) {
        alert('weekend');
    }
});

This is set up with a change event. Your event may be different.

It get's the value of the input: $(this).val()

Creates a new Date object from it: new Date($(this).val())

Then gets the day number: .getDay() which returns a value from 0 to 6 with 0 being Sunday and 6 being Saturday.

Then you just test for 0 or 6.

Live Example: http://jsfiddle.net/UwcLf/


EDIT: Courtesy of Nick Craver, you can disable specific days if you have no need to run alternate code for weekend selections.

From Nick's linked answer: Disable specific days of the week on jQuery UI datepicker

$("input[id^='Date-<%=Model.ID%>']").datepicker({
    beforeShowDay: function(date) {
        var day = date.getDay();
        return [(day != 0 && day != 6)];
    }
})​​​​​;​

Updated Example: http://jsfiddle.net/UwcLf/1/

Added array for disabled days: http://jsfiddle.net/UwcLf/3/

这篇关于如何验证datepicker禁止/拒绝某些日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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