Jquery bassistance:验证结束日期晚于开始日期
我正在使用 jquery bassistance 验证,需要验证开始日期是否在结束日期之后。我想知道是否有人知道此功能是否存在,以及我是否可以添加规则来执行此操作,或者是否必须添加自定义检查。我很难找到与我的代码一起使用的任何示例,如果表单有效,则该代码会转发到另一个函数。 (粘贴在下面)
$("#hotels").validate({
rules: {
checkinDate: {
date: true
},
checkoutDate: {
date: true
}
}
});
$("#hotels input.submit").click(function() {
var isValid = $("#hotels").valid();
if (isValid == true) {
} else {
hComp();
return false;
}
} else {
return false;
}
});
我在另一个示例中看到了这一点,但不确定如何使用它。
var startDate = new Date($('#startDate').val());
var endDate = new Date($('#endDate').val());
if (startDate < endDate){
// Do something
}
预先感谢您的任何帮助。
I am using jquery bassistance validation and need to verify that a start date is after an end date. I am wondering if anyone knows if this functionality exists and if I can add rules to do this or if I have to add a custom check. I am having difficulty finding any examples of this that work with my code which forwards to another function if the form is valid. (pasted below)
$("#hotels").validate({
rules: {
checkinDate: {
date: true
},
checkoutDate: {
date: true
}
}
});
$("#hotels input.submit").click(function() {
var isValid = $("#hotels").valid();
if (isValid == true) {
} else {
hComp();
return false;
}
} else {
return false;
}
});
I saw this in another example, but am not sure how I could use it.
var startDate = new Date($('#startDate').val());
var endDate = new Date($('#endDate').val());
if (startDate < endDate){
// Do something
}
Thanks in advance for any assistance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该示例看起来正是您所需要的。请查看此处。
借用该 jquery 示例,假设您有以下 HTML:
那么此 JavaScript 代码应显示警报“开始日期早于结束日期!” (假设值未修改):
That example looks like it's exactly what you need. Have a look here.
Borrowing from that jquery example, suppose you have the following HTML:
Then this JavaScript code should show the alert "Start date is before end date!" (assuming the values aren't modified):
我最终在验证之前解决了这个问题,无论如何这可能是一个更好的主意。这是使用 jquery ui datepicker 的解决方案。然后我可以使用相同的逻辑进行最终的验证检查。
beforeShow:自定义范围,
显示:“两者”,
ButtonImage: contextPath + '/assets/images/icon-calendar.gif',
仅按钮图像:true,
按钮文本:“显示日期选择器”
});
函数自定义范围(a){
var b = new Date();
var c = new Date(b.getFullYear(), b.getMonth(), b.getDate());
if (a.id == '结帐日期') {
if ($('#checkinDate').datepicker('getDate') != null) {
c = $('#checkinDate').datepicker('getDate');
}
}
返回{
最短日期:c
}
}
I ended up solving this problem before validation, which is probably a much better idea anyway. Here's the solution using the jquery ui datepicker. Then I can just use the same logic to do a final validation check.
beforeShow: customRange,
showOn: 'both',
buttonImage: contextPath + '/assets/images/icon-calendar.gif',
buttonImageOnly: true,
buttonText: 'Show Date Picker'
});
function customRange(a) {
var b = new Date();
var c = new Date(b.getFullYear(), b.getMonth(), b.getDate());
if (a.id == 'checkoutDate') {
if ($('#checkinDate').datepicker('getDate') != null) {
c = $('#checkinDate').datepicker('getDate');
}
}
return {
minDate: c
}
}