如何验证datepicker禁止/拒绝某些日期? [英] How to validate datepicker to forbid/reject certain dates?
问题描述
尝试这个: 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屋!